欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python連接mysql實(shí)例分享

 更新時(shí)間:2016年10月09日 08:45:26   投稿:hebedich  
本文給大家匯總介紹了使用python連接mysql的幾個(gè)實(shí)例,非常的簡(jiǎn)單實(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庫(kù)共:%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沒(méi)有報(bào)錯(cuò),就說(shuō)明安裝成功.

2 下面就可以連接數(shù)據(jù)庫(kù),可以進(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ù)庫(kù)的連接 
conn = MySQLdb.connect(host='localhost',user='root',passwd='davehe') 
#獲取游標(biāo) 
curs = conn.cursor() 
#執(zhí)行SQL,創(chuàng)建一個(gè)數(shù)據(jù)庫(kù) 
curs.execute("create database pythondb") 
#選擇連接哪個(gè)數(shù)據(jù)庫(kù) 
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ù)庫(kù)的連接 
conn = MySQLdb.connect(host='localhost',user='root',passwd='hc1226') 
#獲取游標(biāo) 
curs = conn.cursor() 
#選擇連接哪個(gè)數(shù)據(jù)庫(kù) 
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)指到第二條記錄,下面也就從第二條記錄開(kāi)始返回 
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') 

相關(guān)文章

  • python中的List sort()與torch.sort()

    python中的List sort()與torch.sort()

    這篇文章主要介紹了python中的List sort()與torch.sort()使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • python計(jì)算列表元素與乘積詳情

    python計(jì)算列表元素與乘積詳情

    這篇文章主要介紹了python計(jì)算列表元素與乘積,文章圍繞主題展開(kāi)詳細(xì)內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • Python實(shí)現(xiàn)鏈表反轉(zhuǎn)的方法分析【迭代法與遞歸法】

    Python實(shí)現(xiàn)鏈表反轉(zhuǎn)的方法分析【迭代法與遞歸法】

    這篇文章主要介紹了Python實(shí)現(xiàn)鏈表反轉(zhuǎn)的方法,結(jié)合實(shí)例形式分析了Python迭代法與遞歸法實(shí)現(xiàn)鏈表反轉(zhuǎn)的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2020-02-02
  • python列表:開(kāi)始、結(jié)束、步長(zhǎng)值實(shí)例

    python列表:開(kāi)始、結(jié)束、步長(zhǎng)值實(shí)例

    這篇文章主要介紹了python列表:開(kāi)始、結(jié)束、步長(zhǎng)值實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Python中操作文件之write()方法的使用教程

    Python中操作文件之write()方法的使用教程

    這篇文章主要介紹了Python中操作文件之write()方法的使用教程,是Python入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • PyCharm 專(zhuān)業(yè)版安裝圖文教程

    PyCharm 專(zhuān)業(yè)版安裝圖文教程

    這篇文章主要介紹了PyCharm 專(zhuān)業(yè)版安裝圖文教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • 手把手教你如何使python變?yōu)榭蓤?zhí)行文件

    手把手教你如何使python變?yōu)榭蓤?zhí)行文件

    對(duì)于exe可執(zhí)行文件,相信大家都不陌生,下面這篇文章主要給大家介紹了關(guān)于如何使python變?yōu)榭蓤?zhí)行文件的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02
  • Python操作配置文件ini的三種方法講解

    Python操作配置文件ini的三種方法講解

    今天小編就為大家分享一篇關(guān)于Python操作配置文件ini的三種方法講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-02-02
  • web.py獲取上傳文件名的正確方法

    web.py獲取上傳文件名的正確方法

    這篇文章主要介紹了web.py獲取上傳文件名的正確方法,遇到這個(gè)問(wèn)題的朋友可能會(huì)困惑半天,使用本文的正確方法就可以解決這個(gè)問(wèn)題了,需要的朋友可以參考下
    2014-08-08
  • Python中Pickle模塊和base64模塊的使用解析

    Python中Pickle模塊和base64模塊的使用解析

    這篇文章主要介紹了Python中Pickle模塊和base64模塊的使用解析,pickle模塊是python的標(biāo)準(zhǔn)模塊,提供了對(duì)于python數(shù)據(jù)的序列化操作,可以將數(shù)據(jù)轉(zhuǎn)換為bytes類(lèi)型,其序列化速度比json模塊要高,需要的朋友可以參考下
    2023-09-09

最新評(píng)論