python 對mysql的基本操作

2021-10-04 13:13:13 字數 1308 閱讀 5164

朋友面試,幫用python寫的資料庫基礎操作

import mysqldb

conn = mysqldb.connect(

host=

'localhost'

, port=

3306

, user=

'root'

, passwd=

'mima123456'

, db=

'test1'

, charset=

'utf8'

)c = conn.cursor(

)#游標

# 讀寫資料庫中的資料

deffind11()

: c.execute(

'select * from test1'

)for i in

range

(c.rowcount)

: row = c.fetchone(

)print

(row)

deffind2()

: c.execute(

'select * from test1'

)return c.fetchall(

)# 插入資料

defadd

(x):

for i in

range

(x):

c.execute(f'insert into test1(num,name,old) values ("","",now())'

)print

(find2())

#刪除資料

defdelete

(x):

for i in

range

(x):

c.execute(f'delete from test1 where num = '

)print

(find2())

#修改資料

defupdata

(x):

for i in

range

(x):

c.execute(f"update test1 set name='ljh'where num = "

)print

(find2())

add(3)

#新增3條資料

updata(4)

#修改3條資料

delete(3)

#刪除3條資料

conn.commit(

)conn.close(

)

python對mysql的操作

安裝好了 mysqldb並將其移動到site packages裡面,我們在程式便可以呼叫了。1.與資料庫建立連線 import mysqldb try cxn mysqldb.connect host localhost user root passwd root db python except ...

php對Mysql基本程式設計

1 建立php工程 2 分別定義mysql資料庫的ip位址 使用者名稱 使用者密碼 資料庫名 servername localhost username username password password dbname mysql 3 用方法mysqli connect例項化mysqli 4 用m...

python對mysql的操作二

游標實際上是一種能從包括多條資料記錄的結果集中每次提取一條記錄的機制 游標總是與一條sql 選擇語句相關聯因為游標由結果集 可以是零條 一條或由相關的選擇語句檢索出的多條記錄 和結果集中指向特定記錄的游標位置組成 import mysqldb def connect mysql db config ...