這里使用的是MySQLdb,需要提前安裝mysql-python
代碼:
<pre>
import MySQLdb
打開(kāi)數(shù)據(jù)庫(kù)連接
db = MySQLdb.connect("數(shù)據(jù)庫(kù)地址","用戶(hù)名","密碼","庫(kù)名" )
使用cursor()方法獲取操作游標(biāo)
cursor = db.cursor()
如果數(shù)據(jù)表已經(jīng)存在使用 execute() 方法刪除表。
cursor.execute("INSERT INTO temperature(te) VALUES('15')")
關(guān)閉數(shù)據(jù)庫(kù)連接
db.close()
</pre>