Python 連接 Mysql
連接Mysql
#coding=utf-8
#MySQLdb 示例
#
##################################
import MySQLdb
#建立和數據庫系統的連接
conn = MySQLdb.connect(host='localhost', user='root',passwd='longforfreedom')
#獲取操作游標
cursor = conn.cursor()
#執行SQL,創建一個數據庫.
cursor.execute("""create database python """)
#關閉連接,釋放資源
cursor.close();
插入數據、批量插入數據#MySQLdb 示例
#
##################################
import MySQLdb
#建立和數據庫系統的連接
conn = MySQLdb.connect(host='localhost', user='root',passwd='longforfreedom')
#獲取操作游標
cursor = conn.cursor()
#執行SQL,創建一個數據庫.
cursor.execute("""create database python """)
#關閉連接,釋放資源
cursor.close();
#coding=utf-8
###################################
# @author migle
# @date 2010-01-17
##################################
#MySQLdb 示例
#
##################################
import MySQLdb
#建立和數據庫系統的連接
conn = MySQLdb.connect(host='localhost', user='root',passwd='21ccvn')
#獲取操作游標
cursor = conn.cursor()
#執行SQL,創建一個數據庫.
cursor.execute("""create database if not exists 21ccvn""")
#選擇數據庫
conn.select_db('21ccvn');
#執行SQL,創建一個數據表.
cursor.execute("""create table 21ccvn(id int, info varchar(100)) """)
value = [1,"inserted ?"];
#插入一條記錄
cursor.execute("insert into test values(%s,%s)",value);
values=[]
#生成插入參數值
for i in range(20):
values.append((i,'Hello mysqldb, I am recoder ' + str(i)))
#插入多條記錄
cursor.executemany("""insert into test values(%s,%s) """,values);
#關閉連接,釋放資源
cursor.close();
查詢,獲取一個,獲取多個,獲取所有記錄數###################################
# @author migle
# @date 2010-01-17
##################################
#MySQLdb 示例
#
##################################
import MySQLdb
#建立和數據庫系統的連接
conn = MySQLdb.connect(host='localhost', user='root',passwd='21ccvn')
#獲取操作游標
cursor = conn.cursor()
#執行SQL,創建一個數據庫.
cursor.execute("""create database if not exists 21ccvn""")
#選擇數據庫
conn.select_db('21ccvn');
#執行SQL,創建一個數據表.
cursor.execute("""create table 21ccvn(id int, info varchar(100)) """)
value = [1,"inserted ?"];
#插入一條記錄
cursor.execute("insert into test values(%s,%s)",value);
values=[]
#生成插入參數值
for i in range(20):
values.append((i,'Hello mysqldb, I am recoder ' + str(i)))
#插入多條記錄
cursor.executemany("""insert into test values(%s,%s) """,values);
#關閉連接,釋放資源
cursor.close();
#coding=utf-8
#
# MySQLdb 查詢
#
#######################################
import MySQLdb
conn = MySQLdb.connect(host='localhost', user='root', passwd='longforfreedom',db='21ccvn')
cursor = conn.cursor()
count = cursor.execute('select * from test')
print '總共有 %s 條記錄',count
#獲取一條記錄,每條記錄做為一個元組返回
print "只獲取一條記錄:"
result = cursor.fetchone();
print result
#print 'ID: %s info: %s' % (result[0],result[1])
print 'ID: %s info: %s' % result
#獲取5條記錄,注意由于之前執行有了fetchone(),所以游標已經指到第二條記錄了,也就是從第二條開始的所有記錄
print "只獲取5條記錄:"
results = cursor.fetchmany(5)
for r in results:
print r
print "獲取所有結果:"
#重置游標位置,0,為偏移量,mode=absolute | relative,默認為relative,
cursor.scroll(0,mode='absolute')
#獲取所有結果
results = cursor.fetchall()
for r in results:
print r
conn.close()
#
# MySQLdb 查詢
#
#######################################
import MySQLdb
conn = MySQLdb.connect(host='localhost', user='root', passwd='longforfreedom',db='21ccvn')
cursor = conn.cursor()
count = cursor.execute('select * from test')
print '總共有 %s 條記錄',count
#獲取一條記錄,每條記錄做為一個元組返回
print "只獲取一條記錄:"
result = cursor.fetchone();
print result
#print 'ID: %s info: %s' % (result[0],result[1])
print 'ID: %s info: %s' % result
#獲取5條記錄,注意由于之前執行有了fetchone(),所以游標已經指到第二條記錄了,也就是從第二條開始的所有記錄
print "只獲取5條記錄:"
results = cursor.fetchmany(5)
for r in results:
print r
print "獲取所有結果:"
#重置游標位置,0,為偏移量,mode=absolute | relative,默認為relative,
cursor.scroll(0,mode='absolute')
#獲取所有結果
results = cursor.fetchall()
for r in results:
print r
conn.close()
posted on 2011-08-14 08:46 草原上的駱駝 閱讀(314) 評論(0) 編輯 收藏 所屬分類: Python