1.创建数据库:
打开Mysql命令行程序,
CREATE DATABASE python1;
2.创建用户并赋予权限:
赋予权限时候如果没用户自动创建用户,这样可以省略创建的步骤
grant all privileges on python1.* to cjw@localhost identified by '123456';
3.打开python,输入以下代码:
import MySQLdb
result=[1,2,3]
try:
conn=MySQLdb.connect(host='localhost',user='cjw',passwd='123456',port=3306)
cur=conn.cursor()
conn.select_db('python1')
cur.execute('create table test1(URL varchar(20))')
for t in result:
value=[t]
cur.execute('insert into test1 values(%s)',value)
conn.commit()
cur.close()
conn.close()
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
4.sql语句及结果,如下: