Skip to content

Commit 26933d3

Browse files
committed
Create 0002.py
1 parent 28ce6de commit 26933d3

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

razzl/0002/0002.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
3+
'''
4+
It can save the contents of the file to the mysql database.
5+
'''
6+
7+
import os
8+
import random
9+
import MySQLdb
10+
#import string
11+
12+
f = open(os.getcwd()+'\\2','w')
13+
for x in range(200):
14+
words = [chr(a) for a in range(65,91)]+[chr(a) for a in range(97,122)]+[str(a) for a in range(0,11)]
15+
# It is equal to string.ascii_letters + string.digits
16+
slices = random.sample(words,10)
17+
#temp = str(slices)
18+
temp = "".join(slices)+'\n'
19+
#print temp
20+
f.write(temp)
21+
f.close()
22+
23+
f = open(os.getcwd()+'\\2','r')
24+
words = f.readlines()
25+
try:
26+
conn = MySQLdb.connect(host = 'localhost', user = 'root', passwd = '******', port = 3306)#connet to your local database of mysql
27+
28+
cur = conn.cursor()
29+
30+
conn.select_db('python')
31+
32+
count = cur.executemany('insert into test(name) values(%s)',words)#executemant need two parameters sql statement and list
33+
34+
conn.commit()#commit the implementation results
35+
cur.close()#close the cursor
36+
conn.close()# close the connect
37+
except MySQLdb.Error,e:
38+
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
39+

0 commit comments

Comments
 (0)