python3好用的mysql.connector库

python3好用的mysql.connector库


from mysql.connector import connect

#建立mysql连接,生成一个mysql.connector对象
conn = connect(user='root',password='xxxxxxxx',host='192.168.1.246',database='property')
cursor = conn.cursor()

#新建一个table
cursor.execute('CREATE TABLE `t_user_test` (`id` varchar(64) NOT NULL,`loginName` varchar(64) DEFAULT NULL ,`password` varchar(128) DEFAULT NULL,`username` varchar(64) DEFAULT NULL ,`phaseId` varchar(64) DEFAULT NULL ,`phase` varchar(64) DEFAULT NULL ,`subjectId` varchar(64) DEFAULT NULL ,`subject` varchar(64) DEFAULT NULL ,`email` varchar(128) DEFAULT NULL ,`mobile` varchar(16) DEFAULT NULL ,`sex` varchar(2) DEFAULT NULL ,`birthday` date DEFAULT NULL ,`teachAge` varchar(10) DEFAULT NULL ,`level` varchar(11) DEFAULT NULL ,`state` varchar(2) DEFAULT NULL ,`createTime` datetime DEFAULT NULL ,`updateTime` datetime DEFAULT NULL ,`headPicPath` varchar(128) DEFAULT NULL ,`type` varchar(2) DEFAULT NULL ,`profile` varchar(200) DEFAULT NULL ,PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8')

#向数据库表格中,循环插入多条信息
def insertuser(minnum,maxnum):
    for i in range(minnum,maxnum+1):
        userid = 'test'+str(i).zfill(4)
        loginname = name = userid
        email = userid+ '@qq.com'
        listuser = [userid, loginname,'DD4B21E9EF71E1291183A46B913AE6F2', name, email,  '1', '2017-03-27 11:11:11', '../images/defaultPhoto.png','2']
        cursor.execute('insert into `t_user_front` VALUES (%s, %s, %s, %s, \'value1\', \'value2\', \'value3\',\'value4\', %s, null, null, null, null, null, %s, %s, null, %s, %s, null)',listuser)
    conn.commit()

#进行查询操作
cursor.execute('select * from t_user_test')
values = cursor.fetchall()      #返回一个列表,values,每个查询结果会以tuple的形式存储

#删除数据
cursor.execute('delete from t_user_test where id = \'001\'')
conn.commit()

conn.close()





最近我电脑上都mysql-connector出了点问题,重新安装安装不了,报错,去mysql官网,使用mysql-connector安装命令也不行,后来在网上看见有人说

pip install --upgrade pip  
pip install --upgrade wheel
pip install mysql-connector-python-rf

之后就行了,试了一下,果然行了。

posted on 2017-03-27 17:21  胡欣欣  阅读(474)  评论(0编辑  收藏  举报