您的位置:首页 > 编程语言 > Python开发

python3连接Mysql数据库实例

2014-07-04 17:20 148 查看
// 准备工作, 在mysql网站上面下载
mysql-connector-python-1.1.6-py3.2.msi

#------------- 代码
import mysql.connector
import sys, os

user = 'root'
pwd = '123456'
host = '127.0.0.1'
db = 'test'

data_file = 'mysql-test.dat'

create_table_sql = "CREATE TABLE IF NOT EXISTS mytable ( \
id int(10) AUTO_INCREMENT PRIMARY KEY, \
name varchar(20), age int(4) ) \
CHARACTER SET utf8"

insert_sql = "INSERT INTO mytable(name, age) VALUES ('Jay', 22 ), ('杰', 26)"
select_sql = "SELECT id, name, age FROM mytable"

cnx = mysql.connector.connect(user=user, password=pwd, host=host, database=db)
cursor = cnx.cursor()

try:
cursor.execute(create_table_sql)
except mysql.connector.Error as err:
print("create table 'mytable' failed.")
print("Error: {}".format(err.msg))
sys.exit()

try:
cursor.execute(insert_sql)
except mysql.connector.Error as err:
print("insert table 'mytable' failed.")
print("Error: {}".format(err.msg))
sys.exit()

if os.path.exists(data_file):
myfile = open(data_file)
lines = myfile.readlines()
myfile.close()

for line in lines:
myset = line.split()
sql = "INSERT INTO mytable (name, age) VALUES ('{}', {})".format(myset[0], myset[1])
try:
cursor.execute(sql)
except mysql.connector.Error as err:
print("insert table 'mytable' from file 'mysql-test.dat' -- failed.")
print("Error: {}".format(err.msg))
sys.exit()

try:
cursor.execute(select_sql)
for (id, name, age) in cursor:
print("ID:{} Name:{} Age:{}".format(id, name, age))
except mysql.connector.Error as err:
print("query table 'mytable' failed.")
print("Error: {}".format(err.msg))
sys.exit()

cnx.commit()
cursor.close()
cnx.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: