python3 使用pymysql
2016-09-04 17:37
169 查看
#! /usr/bin/env python3 # coding = utf-8 import random import pymysql # 连接数据库函数 def connDB(data): conn = pymysql.connect(host='localhost',user='root',passwd='#######',db='test',) #数据库 cur = conn.cursor() #游标 cur.execute('create database if not exists test;') #执语句行 cur.execute('create table if not exists test1(id INT NOT NULL, num VARCHAR(40) );') for i in range(len(data)): cur.execute('insert into test1 (id,num) values("{0}","{1}");'.format(i,data[i])) #{0} {1} 要和sql语句区分 cur.close() #关游标 conn.commit() conn.close() #关数据库 # 产生激活码 def make_number(num,length): lstr = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' a = [] cnt = 0 while cnt < num: a_str = '' for j in range(length): a_str += random.choice(lstr) if a_str not in a: a.append(a_str) cnt +=1 return a if __name__ == "__main__": nums = make_number(100,20) print(nums) connDB(nums)
错误处理:
pymysql.err.InternalError: (1054, "Unknown column 'K0F3hNCZUrXIA4wMEk6a' in 'field list'")
{0} {1} 要和sql语句区分,所以在该语句中用双引号标注,其他格式化字符串(d%,s%等也应用引号区分)
相关文章推荐
- django1.6.x(python3.3)使用pymysql连接mysql
- Python3.4下使用pymysql插入数据后没有反应的问题
- Python pymysql的基本使用方法
- python3使用PyMysql连接mysql数据库实例
- python使用pymysql操作mysql数据库
- Python 3.2: 使用pymysql连接Mysql
- Python3.5-Django1.11 使用pymysql 连接mysql
- python使用pymysql操作数据库
- Python3.4使用pymysql连接MySQL
- Python进阶(二十三)-Django使用pymysql连接MySQL数据库做增删改查
- 使用pymysql在python中对mysql的增删改查操作(一)
- 使用pymysql在python中对mysql的增删改查操作(综合)
- 使用pymysql在python中对mysql的增删改查操作(五)
- Python3中使用PyMySQL连接Mysql
- python3使用PyMysql连接mysql数据库
- python3.4 使用pymysql 连接mysql数据库
- 使用pymysql在python中对mysql的增删改查操作(四)
- python3.4使用PyMySQL来连接mysql
- Python 使用pymysql连接MySQL
- python 使用MySQLdb 和 pymysql的时候手动提交事务的解决办法