一、基本概念
使用python操作數據庫,其基本的流程如下(其實所有開發語言訪問數據庫的流程都是這樣)。
1、第一,引入相應數據庫的python數據庫接口模塊,針對不同的數據庫類型,有不同的數據庫訪問接口模塊。可以理解這些接口模塊提供了一些api接口,讓python代碼可以訪問數據庫。
2、獲取數據庫的連接
3、執行sql語句
4、關閉數據庫連接
二、下載python數據庫接口包
比如對應mysql數據庫,首先要下載相應的python mysql包。
可以到 https://pypi.python.org/pypi/MySQL-python/1.2.5 這個鏈接下下載,
對於windows系統,會有exe安裝包,安裝后,會在 Python27\Lib\site-packages 目錄下生成mysql的包
三、代碼編寫
表結構如下: create table test(id int,info varchar(100))
#coding=utf-8 import time import MySQLdb try: conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306) cur=conn.cursor() cur.execute('insert into test values(0,"x0")') conn.commit() cur.close() conn.close() print "finish insert direct" except MySQLdb.Error,e: print e.args[1] try: conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306) cur=conn.cursor() cur.execute('insert into test values(%s,%s)',(1,"x1")) cur.execute('insert into test values(%s,%s)',[2,"x2"]) conn.commit() cur.close() conn.close() print "finish insert by para" except MySQLdb.Error,e: print e.args[1] try: conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306) cur=conn.cursor() data=[]; data.append((3,"x3")); data.append((4,"x4")); cur.executemany('insert into test values(%s,%s)',data) data=[]; data.append([5,"x5"]); data.append([6,"x6"]); cur.executemany('insert into test values(%s,%s)',data) conn.commit() cur.close() conn.close() print "finish muti insert by para" except MySQLdb.Error,e: print e.args[1] try: conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306) cur=conn.cursor() cur.execute('select * from test where id=%s',('2')) #數字參數? res=cur.fetchall() for re in res: print "id=%s,info=%s" % (re[0],re[1]) cur.close() conn.close() print "finish query where" except MySQLdb.Error,e: print e.args[1] try: conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306) cur=conn.cursor() cur.execute('select * from test') res=cur.fetchall() for re in res: print "id=%s,info=%s" % (re[0],re[1]) cur.close() conn.close() print "finish query fetchall" except MySQLdb.Error,e: print e.args[1] try: conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306) cur=conn.cursor() cur.execute('select * from test') #游標指向第一條記錄 re = cur.fetchone() #獲取當前游標記錄,同時游標指向下一條記錄 print "id=%s,info=%s" % (re[0],re[1]) re = cur.fetchone() print "id=%s,info=%s" % (re[0],re[1]) res=cur.fetchall() #獲取當前游標記錄及后續所有記錄 for re in res: print "id=%s,info=%s" % (re[0],re[1]) cur.close() conn.close() print "finish query fetchone" except MySQLdb.Error,e: print e.args[1] try: conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306) cur=conn.cursor() cur.execute('delete from test') conn.commit() cur.close() conn.close() print "finish delete" except MySQLdb.Error,e: print e.args[1]
需要說明的是,如果python腳本的字符集編碼與數據庫的字符集不一致,中文會出現亂碼。
解決方法是在獲取鏈接時指定數據庫的字符集。如:
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='',db='dpms',port=3306,charset='gbk')
上面語句通過 增加 charset='gbk' 指定數據庫的字符集。