Python Mysql数据库连接

import MySQLdb
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='redhat',db='test',port=3306)
cur = conn.cursor()
#cur.execute('select * from test')
cur.execute('insert into test value(3,13)')
#print cur.fetchone()
print  cur.fetchall()
cur.close()
conn.commit()
conn.close()


conn=MySQLdb.connect(host='127.0.0.1',user='root',passwd='redhat',port=3306)
cur=conn.cursor()
cur.execute('create database if not exists test1')
conn.select_db('test1')
cur.execute('create table test(id int,info varchar(20))')
value=[1,'hi one']
cur.execute('insert into test values(%s,%s)',value)
    values=[]
    for i in range(20):
        values.append((i,'hi one'+str(i)))
    cur.executemany('insert into test values(%s,%s)',values)
    cur.execute('update test set info="I am FreeMan" where id=3')
    conn.commit()
    cur.close()
    conn.close()

posted @ 2016-04-19 19:34  FreeMan1  阅读(220)  评论(0编辑  收藏  举报