-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2. MySQLdb_1c7.py
More file actions
48 lines (20 loc) · 1 KB
/
Copy path2. MySQLdb_1c7.py
File metadata and controls
48 lines (20 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#coding=utf-8
import MySQLdb
'''
http://www.cnblogs.com/rollenholt/archive/2012/05/29/2524327.html
http://mysql-python.sourceforge.net/MySQLdb.html
http://mysql-python.sourceforge.net/MySQLdb-1.2.2/public/MySQLdb-module.html
The DB API specification PEP-249 should be your primary guide for using this module. Only deviations from the spec and other database-dependent things will be documented here.
https://www.python.org/dev/peps/pep-0249/#cursor-methods
'''
db = MySQLdb.connect(host='localhost',user='root',passwd='admin',db='tophot',port=3306) #don't use charset here
# setup a cursor object using cursor() method
cursor = db.cursor()
cursor.execute("SET NAMES utf8mb4;") #or utf8 or any other charset you want to handle
cursor.execute("SET CHARACTER SET utf8mb4;") #same as above
cursor.execute("SET character_set_connection=utf8mb4;") #same as above
a = cursor.execute("SELECT * FROM question") # 返回记录条数
print (a)
print (cursor.fetchone())
for x in cursor.fetchall():
print (x)