`

@Marked-python插入数据库mysql

 
阅读更多
#-*- coding:utf-8 -*-
import MySQLdb
#alter table test add index prefixIdx_test(ext(2));//前缀索引
try:
	# 1.初始化连接
	conn=MySQLdb.connect(host='localhost',user='root',passwd='1234',port=3306)
	# 2.使用连接获取游标
	cur=conn.cursor()
	cur.execute('create database if not exists python')
	conn.select_db('python')
	cur.execute('create table test(id int,info varchar(20),ext varchar(200))')

	value=[1,'hi python','__welcomeUhere@ToOurNewWebSite!!!']
	cur.execute('insert into test values(%s,%s,%s)',value)

	values=[]
	for i in range(10000):
		values.append((i,'hi python'+str(i), str(i)+'__welcomeUhereToOurNewWebSite!!!'))
	cur.executemany('insert into test values(%s,%s,%s)',values)
	conn.commit()
	cur.close()
	conn.close()
except MySQLdb.Error,e:
	print "Mysql Error %d: %s" % (e.args[0], e.args[1])

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics