pymysql連線資料庫

2021-08-17 16:43:33 字數 3764 閱讀 4275



建立資料庫

import pymysql

# 開啟資料庫連線

db = pymysql.connect("localhost","testuser","test123","testdb" )

# 使用 cursor() 方法建立乙個游標物件 cursor

cursor = db.cursor()

# 使用 execute() 方法執行 sql,如果表存在則刪除

cursor.execute("drop table if exists employee")

# 使用預處理語句建立表

sql = """create table employee (

first_name  char(20) not null,

last_name  char(20),

age int,  

*** char(1),

income float )"""

cursor.execute(sql)

# 關閉資料庫連線

db.close()

import pymysql

def createtable():

#建立鏈結

conn=pymysql.connect("localhost","root","123456","school")

#得到cursor

mycursor=conn.cursor()

#執行sql語句

sqlstr='''

create table person2(

id int primary key auto_increment,

name varchar(20) not null,

*** char(2)

)'''

mycursor.execute(sqlstr)

#獲取執行的結果,如果建立表的語句則返回none

#關閉連線物件

conn.close()

#createtable()

#資料插入

def inserttable():

conn=pymysql.connect("localhost","root","123456","school")

mycursor=conn.cursor()

sqlstr='''insert into person2(name,***) values('張三','男')'''.encode("utf-8")

try:

result=mycursor.execute(sqlstr)

print(result)

conn.commit()#提交到資料庫執行

except:

conn.rollback()#如果發生錯誤則回滾

inserttable()

#公共連線

def getconn():

conn = pymysql.connect("localhost", "root", "123456", "school")

mycursor=conn.cursor()

return [conn,mycursor]

#刪除def deltable():

conn=getconn()

sqlstr='''delete from person2 where id=1'''

try:

print(conn[1].execute(sqlstr))

conn[0].commit()

except:

conn[0].rollback()

conn[0].close()

deltable()

#更新def updatetable():

conn=getconn()

sqlstr='''update person2 set name='李四' where id=2'''

try:

print(conn[1].execute(sqlstr))

conn[0].commit()

except:

conn[0].rollback()

conn[0].close()

updatetable()

#查詢def querytable():

conn=getconn()

sqlstr="select studentno,studentname,***,phone from student1"

conn[1].execute(sqlstr)

rs=conn[1].fetchall()

for row in rs:

print("studentno:%d,studentname:%s,***:%s,phone:%s"%(row[0],row[1],row[2],row[3]))

conn[0].close()

querytable()

#查詢class test():

def __init__(self,studentno=none,studentname=none,***=none,phone=none):

self.studentno=studentno

self.studentname = studentname

self.*** = ***

self.phone = phone

def __str__(self):

return "studentno:"+str(self.studentno)+",studentname:"+self.studentname+",***:"+self.***+",phone:"+self.phone

def querytable():

list=

conn=getconn()

sqlstr="select studentno,studentname,***,phone from student1"

conn[1].execute(sqlstr)

rs=conn[1].fetchall()

for row in rs:

test=test(row[0],row[1],row[2],row[3])

conn[0].close()

return list

testlist=querytable()

for row in testlist:

print(row)

簡便的寫法:

#公共連線

def getconn():

conn = pymysql.connect("localhost", "root", "123456", "school")

mycursor=conn.cursor()

return [conn,mycursor]

def operationtable(sqlstr):

conn = getconn()

try:

print(conn[1].execute(sqlstr))

conn[0].commit()

except:

conn[0].rollback()

conn[0].close()

#刪除# sqlstr = '''delete from person2 where id=1'''

#更新# sqlstr='''update person2 set name='李四' where id=2'''

#增sqlstr='''insert into person2(name,***) values ('張三','男')'''

operationtable(sqlstr)

PyMysql連線資料庫

1 先安裝pymysql模組 pip install pymysql2 匯入pymysql模組 3 連線資料庫 conn pymysql.connect host localhost user root passwd 123456 port 3306 db test1 charset utf8 cu...

pymysql連線資料庫

pymysql連線資料庫的步驟 1.匯入pymysql 2.使用 pymysql.connect host 位址,user 使用者名稱,password 密碼,port 埠,db 資料庫名 建立資料庫的連線,得到連線物件 3.獲取游標物件 con.cursor pymysql.cursors.dic...

pymysql連線資料庫異常

一 源 如下 import pandas as pd import pymysql 開啟資料庫連線 host address user 密碼 庫名 編碼 db pymysql.connect localhost root 123456 test charset utf8 出現異常 typeerror...