Pythonによるmysqlデータベースの操作
3278 ワード
Pythonによるmysqlデータベースの操作
一般的な関数:
その後、この接続オブジェクトはトランザクション操作のサポート、標準的な方法も提供します.
commit()コミット
ロールバック
cursorがコマンドを実行する方法:
callproc(self,procname,args):ストレージ・プロシージャを実行するために使用され、受信したパラメータはストレージ・プロシージャ名とパラメータ・リストであり、戻り値は影響を受けるロー数である
execute(self,query,args):単一のsql文を実行し、受信したパラメータはsql文自体と使用するパラメータリストであり、戻り値は影響を受ける行数である
executemany(self,query,args):単一のsql文を実行しますが、パラメータリストのパラメータを繰り返し実行し、影響を受ける行数を返します.
nextset(self):次の結果セットに移動
Cursorが戻り値を受信する方法:
fetchall(self):すべての戻り結果行を受信.
fetchmany(self,size=None):sizeバーから結果行を返す.sizeの値が返される結果行の数より大きい場合、cursorが返されます.Arraysizeバーのデータ
fetchone(self):結果行を返します.
scroll(self,value,mode='llative'):ポインタを行に移動します.mode='rlative'の場合、現在の行からvalueバーを移動することを表し、mode='absolute'の場合、結果セットの最初の行からvalueバーを移動することを表す.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import MySQLdb
class mysql:
def __init__(self, sql, host='127.0.0.1', username='root', password='root', dbname='dbname'):
self.username = username
self.password = password
self.dbname = dbname
self.sql = sql
self.mysqldb = MySQLdb.connect(host, self.username, self.password, self.dbname, charset="utf8")
#
def query(self):
try:
cursor = self.mysqldb.cursor()
cursor.execute(self.sql)
data = cursor.fetchall()
return data
except Exception as e:
print e
#
def insert(self):
try:
cursor = self.mysqldb.cursor()
cursor.execute(self.sql)
self.mysqldb.commit()
self.mysqldb.close()
return 'ok'
except Exception as e:
print e
#
def delete(self):
try:
cursor = self.mysqldb.cursor()
cursor.execute(self.sql)
self.mysqldb.commit()
self.mysqldb.close()
except Exception as e:
print e
#
def update(self):
try:
cursor = self.mysqldb.cursor()
cursor.execute(self.sql)
self.mysqldb.commit()
self.mysqldb.close()
except Exception as e:
print e
if __name__=="__main__":
pass
#!/usr/bin/python
# -*- coding: utf-8 -*-
__author__ = 'gaogd'
import MySQLdb
try:
conn = MySQLdb.connect(host='localhost', user='root', passwd='root', port=3306)
cur = conn.cursor()
cur.execute('create database if not exists python')
conn.select_db('python')
cur.execute('create table test(id int,info varchar(20))')
value = [1, 'hi rollen']
cur.execute('insert into test values(%s,%s)', value)
values = []
for i in range(20):
values.append((i, 'hi rollen' + str(i)))
cur.executemany('insert into test values(%s,%s)', values)
## : cur.executemany
cur.execute('update test set info="I am rollen" where id=3')
conn.commit()
cur.close()
conn.close()
except MySQLdb.Error, e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
一般的な関数:
その後、この接続オブジェクトはトランザクション操作のサポート、標準的な方法も提供します.
commit()コミット
ロールバック
cursorがコマンドを実行する方法:
callproc(self,procname,args):ストレージ・プロシージャを実行するために使用され、受信したパラメータはストレージ・プロシージャ名とパラメータ・リストであり、戻り値は影響を受けるロー数である
execute(self,query,args):単一のsql文を実行し、受信したパラメータはsql文自体と使用するパラメータリストであり、戻り値は影響を受ける行数である
executemany(self,query,args):単一のsql文を実行しますが、パラメータリストのパラメータを繰り返し実行し、影響を受ける行数を返します.
nextset(self):次の結果セットに移動
Cursorが戻り値を受信する方法:
fetchall(self):すべての戻り結果行を受信.
fetchmany(self,size=None):sizeバーから結果行を返す.sizeの値が返される結果行の数より大きい場合、cursorが返されます.Arraysizeバーのデータ
fetchone(self):結果行を返します.
scroll(self,value,mode='llative'):ポインタを行に移動します.mode='rlative'の場合、現在の行からvalueバーを移動することを表し、mode='absolute'の場合、結果セットの最初の行からvalueバーを移動することを表す.