【python学习.用python将数据导入mysql测试】

来源:互联网 发布:如何使用网络电视 编辑:程序博客网 时间:2024/06/07 10:58

1.创建数据库:

打开Mysql命令行程序,

CREATE DATABASE python1;

2.创建用户并赋予权限:

赋予权限时候如果没用户自动创建用户,这样可以省略创建的步骤

grant all privileges on python1.* to cjw@localhost identified by '123456';

3.打开python,输入以下代码:

import MySQLdbresult=[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语句及结果,如下:

1


0 0