python連接mysql實(shí)例分享
示例一
#coding=UTF-8
import sys
import MySQLdb
import time
reload(sys)
sys.setdefaultencoding('utf-8')
def connectDemo():
return MySQLdb.Connection("127.0.0.1","root","root","demo",3306,charset="utf8")
if __name__ == '__main__':
begin=time.time()
conn=connectDemo()
cursor = conn.cursor()
sql="""
show tables
"""
count = cursor.execute(sql)
rows = cursor.fetchall()
cursor.close()
conn.close()
print "========demo庫共:%s 張表============" % (count)
print '耗時(shí):%s 秒' % (time.time()-begin)
示例二
import MySQLdb
conn = MySQLdb.connect(host="localhost",
user="root",
passwd="123456",
db="test")
cursor = conn.cursor()
cursor.execute("select * from hard")
res = cursor.fetchall()
for x in res:
print x
cursor.close()
conn.close()
示例三
1 安裝Python的Mysql包
root@10.1.1.45:~# apt-get install python-mysqldb root@10.1.1.45:~# python Python 2.5.2 (r252:60911, Jan 4 2009, 21:59:32) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb >>>
這里導(dǎo)入MySQLdb沒有報(bào)錯(cuò),就說明安裝成功.
2 下面就可以連接數(shù)據(jù)庫,可以進(jìn)行增刪改操作.
root@10.1.1.45:python# cat create.py
#!/usr/bin/env python
#coding=utf-8
#導(dǎo)入相關(guān)模塊
import MySQLdb
#建立和mysql數(shù)據(jù)庫的連接
conn = MySQLdb.connect(host='localhost',user='root',passwd='davehe')
#獲取游標(biāo)
curs = conn.cursor()
#執(zhí)行SQL,創(chuàng)建一個(gè)數(shù)據(jù)庫
curs.execute("create database pythondb")
#選擇連接哪個(gè)數(shù)據(jù)庫
conn.select_db('pythondb')
#執(zhí)行SQL,創(chuàng)建一個(gè)表
curs.execute("create table test(id int,message varchar(50))")
#插入一條記錄
value = [1,"davehe"]
curs.execute("insert into test values(%s,%s)",value)
#插入多條記錄
values = []
for i in range(20):
values.append((i,'hello mysqldb' + str(i)))
curs.executemany("insert into test values(%s,%s)",values)
#提交修改
conn.commit()
#關(guān)閉游標(biāo)連接,釋放資源
curs.close()
#關(guān)閉連接
conn.close()
root@10.1.1.45:python# ./create.py
3 下面利用python查看mysql里剛添加的記錄.
root@10.1.1.45:python# cat select.py
#!/usr/bin/env python
#coding=utf-8
#導(dǎo)入相關(guān)模塊
import MySQLdb
#建立和mysql數(shù)據(jù)庫的連接
conn = MySQLdb.connect(host='localhost',user='root',passwd='hc1226')
#獲取游標(biāo)
curs = conn.cursor()
#選擇連接哪個(gè)數(shù)據(jù)庫
conn.select_db('pythondb')
#查看共有多少條記錄
count = curs.execute('select * from test')
print "一共有%s條記錄" % count
#獲取一條記錄,以一個(gè)元組返回
result = curs.fetchone()
print "當(dāng)前的一條記錄 ID:%s message:%s" % result
#獲取后10條記錄,由于之前執(zhí)行了getchone(),所以游標(biāo)已經(jīng)指到第二條記錄,下面也就從第二條記錄開始返回
results = curs.fetchmany(10)
for r in results:
print r
#重置游標(biāo)位置,0,為偏移量,mode = relative(默認(rèn))
curs.scroll(0,mode='absolute')
#獲取所有記錄
results = curs.fetchall()
for r in results:
print r
#提交修改
conn.commit()
#關(guān)閉游標(biāo)連接,釋放資源
curs.close()
#關(guān)閉連接
conn.close()
root@10.1.1.45:python# ./select.py 一共有21條記錄 當(dāng)前的一條記錄 ID:1 message:davehe (0L, 'hello mysqldb0') (1L, 'hello mysqldb1') (2L, 'hello mysqldb2') (3L, 'hello mysqldb3') (4L, 'hello mysqldb4') (5L, 'hello mysqldb5') (6L, 'hello mysqldb6') (7L, 'hello mysqldb7') (8L, 'hello mysqldb8') (9L, 'hello mysqldb9') (1L, 'davehe') (0L, 'hello mysqldb0') (1L, 'hello mysqldb1') (2L, 'hello mysqldb2') (3L, 'hello mysqldb3') (4L, 'hello mysqldb4') (5L, 'hello mysqldb5') (6L, 'hello mysqldb6') (7L, 'hello mysqldb7') (8L, 'hello mysqldb8') (9L, 'hello mysqldb9') (10L, 'hello mysqldb10') (11L, 'hello mysqldb11') (12L, 'hello mysqldb12') (13L, 'hello mysqldb13') (14L, 'hello mysqldb14') (15L, 'hello mysqldb15') (16L, 'hello mysqldb16') (17L, 'hello mysqldb17') (18L, 'hello mysqldb18') (19L, 'hello mysqldb19')
- Python實(shí)現(xiàn)MySQL操作的方法小結(jié)【安裝,連接,增刪改查等】
- Python MySQL數(shù)據(jù)庫連接池組件pymysqlpool詳解
- python3使用PyMysql連接mysql數(shù)據(jù)庫實(shí)例
- win7上python2.7連接mysql數(shù)據(jù)庫的方法
- Python的SQLalchemy模塊連接與操作MySQL的基礎(chǔ)示例
- Python連接MySQL并使用fetchall()方法過濾特殊字符
- linux下python3連接mysql數(shù)據(jù)庫問題
- python連接MySQL數(shù)據(jù)庫實(shí)例分析
- 連接Python程序與MySQL的教程
- 用 Python 連接 MySQL 的幾種方式詳解
相關(guān)文章
Python 讀取用戶指令和格式化打印實(shí)現(xiàn)解析
這篇文章主要介紹了Python 讀取用戶指令和格式化打印實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
對(duì)python遍歷文件夾中的所有jpg文件的實(shí)例詳解
今天小編就為大家分享一篇對(duì)python遍歷文件夾中的所有jpg文件的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12
對(duì)Python 文件夾遍歷和文件查找的實(shí)例講解
下面小編就為大家分享一篇對(duì)Python 文件夾遍歷和文件查找的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04
python3 小數(shù)位的四舍五入(用兩種方法解決round 遇5不進(jìn))
這篇文章主要介紹了python3 小數(shù)位的四舍五入(用兩種方法解決round 遇5不進(jìn)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Python使用difflib標(biāo)準(zhǔn)庫實(shí)現(xiàn)查找文本間的差異
在文本處理和比較中,查找文本之間的差異是一項(xiàng)常見的任務(wù),本文將詳細(xì)介紹如何使用difflib模塊來查找文本之間的差異,包括單行和多行文本的比較、生成差異報(bào)告,需要的可以參考下2024-03-03
python 使用cx-freeze打包程序的實(shí)現(xiàn)
這篇文章主要介紹了python 使用cx-freeze打包程序的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
pygame游戲之旅 調(diào)用按鈕實(shí)現(xiàn)游戲開始功能
這篇文章主要為大家詳細(xì)介紹了pygame游戲之旅的第12篇,教大家調(diào)用按鈕實(shí)現(xiàn)游戲開始功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Python 如何實(shí)時(shí)向文件寫入數(shù)據(jù)(附代碼)
這篇文章主要介紹了Python 如何實(shí)時(shí)向文件寫入數(shù)據(jù)(附代碼),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07

