Python????MySQL?????
???????????? ???????[ 2015/8/5 11:12:26 ] ?????????????
????????????????????????????????????????????????????????????д????????????????????????python????MYSQL??????????????
????????????MySQLdb??????MYSQL????????????????????????
????import MySQLdb
????try:
????conn=MySQLdb.connect(host='localhost'??user='root'??passwd='root'??db='test'??port=3306)
????cur=conn.cursor()
????cur.execute('select * from user')
????cur.close()
????conn.close()
????except MySQLdb.Error??e:
????print "Mysql Error %d: %s" % (e.args[0]?? e.args[1])
???????????????????????????????????????
???????????????????2????????????????????????????????????
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.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])
???????????????conn.commit()?????????????????????????????????
??????????????MySQL????????????????
import MySQLdb
try:
conn=MySQLdb.connect(host='localhost'??user='root'??passwd='root'??port=3306)
cur=conn.cursor()
conn.select_db('python')
count=cur.execute('select * from test')
print 'there has %s rows record' % count
result=cur.fetchone()
print result
print 'ID: %s info %s' % result
results=cur.fetchmany(5)
for r in results:
print r
print '=='*10
cur.scroll(0??mode='absolute')
results=cur.fetchall()
for r in results:
print r[1]
conn.commit()
cur.close()
conn.close()
except MySQLdb.Error??e:
print "Mysql Error %d: %s" % (e.args[0]?? e.args[1])
???????н???????????????
???????????????????????????????????????????????????????????????????????п????
??????Python????
????conn = MySQLdb.Connect(host='localhost'?? user='root'?? passwd='root'?? db='python') ?м?????????
?????????
????conn = MySQLdb.Connect(host='localhost'?? user='root'?? passwd='root'?? db='python'??charset='utf8')
????charset?????????????????????????????????gb2312 ????дcharset='gb2312'??
????????????3??????????
????????????????????????????????????????????
????commit() ??
????rollback() ???
????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='relative'):??????????.???mode='relative'???????????????????value??????? mode='absolute'?????????????????????value??.
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11