python中怎么操作mysql数据库
展开全部
#连接数据库
conn = MySQLdb.Connect(
host = 'localhost',
port = 3306,
user = 'root',
passwd = '',
db = 'mytest',
charset = 'utf8'
)
cursor = conn.cursor()
#创建一个表存储数据
sql_create = "create table if not exists account(nameid int, money varchar(100)) "
#插入两条数据
sql_insert1 = "insert into account(nameid,money) values(13,120)"
sql_insert2 = "insert into account(nameid,money) values(14,10)"
#执行上述sql语句
cursor.execute(sql_create)
cursor.execute(sql_insert1)
cursor.execute(sql_insert2)
conn.close()
conn = MySQLdb.Connect(
host = 'localhost',
port = 3306,
user = 'root',
passwd = '',
db = 'mytest',
charset = 'utf8'
)
cursor = conn.cursor()
#创建一个表存储数据
sql_create = "create table if not exists account(nameid int, money varchar(100)) "
#插入两条数据
sql_insert1 = "insert into account(nameid,money) values(13,120)"
sql_insert2 = "insert into account(nameid,money) values(14,10)"
#执行上述sql语句
cursor.execute(sql_create)
cursor.execute(sql_insert1)
cursor.execute(sql_insert2)
conn.close()
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询