Python中操作MySQL入門實例
一、安裝MySQL-python
# yum install -y MySQL-python
二、打開數(shù)據(jù)庫連接
#!/usr/bin/python
import MySQLdb
conn = MySQLdb.connect(user='root',passwd='admin',host='127.0.0.1')
conn.select_db('test')
cur = conn.cursor()
三、操作數(shù)據(jù)庫
def insertdb():
sql = 'insert into test(name,`sort`) values ("%s","%s")'
exsql = sql % ('hello','python')
cur.execute(exsql)
conn.commit()
return 'insert success'
def selectdb():
sql = 'select `name` from test where `sort` = "%s"'
exsql = sql % ('python')
count = cur.execute(exsql)
for row in cur:
print row
print 'cursor move to top:'
cur.scroll(0,'absolute')
row = cur.fetchone()
while row is not None:
print row
row = cur.fetchone()
print 'cursor move to top:'
cur.scroll(0,'absolute')
many = cur.fetchmany(count)
print many
def deletedb():
sql = 'delete from test where `sort` = "%s"'
exsql = sql % ('python')
cur.execute(exsql)
conn.commit()
return 'delete success'
print insertdb()
print insertdb()
selectdb()
print deletedb()
四、關(guān)閉連接
cur.close()
conn.close()
注意順序。
相關(guān)文章
keras打印loss對權(quán)重的導數(shù)方式
這篇文章主要介紹了keras打印loss對權(quán)重的導數(shù)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python利用matplotlib繪制圓環(huán)圖(環(huán)形圖)的實戰(zhàn)案例
環(huán)形圖也被稱為圓環(huán)圖,它在功能上與餅圖相同,只是中間有一個空白,并且能夠同時支持多個統(tǒng)計數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Python利用matplotlib繪制圓環(huán)圖的實戰(zhàn)案例,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-08-08