首页 > 代码库 > python操作mysql数据库

python操作mysql数据库

连接数据库 输入值 存入数据库 关闭

import stringimport mysql.connectorconn=mysql.connector.connect(user=root,password=test,database=dalian,use_unicode=True)cursor=conn.cursor()a=raw_input(enter an id: )b=raw_input(enter a name: )while(a!=quit or b!=quit):    print a    print b    cursor.execute(insert into hi(id,name) values(%s,%s),[a,b])    cursor.rowcount    conn.commit()    cursor.execute(select * from hi)    values=cursor.fetchall()    print values    a=raw_input(enter an id: )    b=raw_input(enter a name: )else:    print donecursor.close()conn.close()