环境
安装 $ sudo apt-get install mysql-server 2-MySQL-python sudo apt-get install python-setuptools sudo apt-get install libmysqld-dev sudo apt-get install libmysqlclient-dev sudo apt-get install python-dev sudo easy_install mysql-python 安装成功之后,终端进入python,然后测试 ![]()
1.png
测试数据库 ![]()
2.png
![]()
3.png
Connect to MySQL ![]()
4.png
Create table user ![]()
5.png
Insert record into your table user ![]()
6.png
Output the record from your table user ![]()
7.png
Run the program ![]()
8.png
小例子
import sys
import MySQLdb
db = MySQLdb.connect(host='localhost',user='root',passwd='root',db='hello')
cursor = db.cursor()
cursor.execute("SELECT VERSION()")
data = cursor.fetchone()
# print "Database version : %s " % data
#---------------------------------------------------
# sql = "create table if not exists test2(name varchar(128) primary key, age int(4))"
# cursor.execute(sql)
sql = "insert into test2(name, age) values ('%s', %d)" % ("ccc", 21)
try:
cursor.execute(sql)
except Exception, e:
print e
db.commit()
sql = "select * from test2"
cursor.execute(sql)
alldata = cursor.fetchall()
if alldata:
for rec in alldata:
print rec[0], rec[1]
db.close()
(责任编辑:最模板) |









麦包包网模板|ecshop包类模
人气:1501
酒美网模板之shopex免费模
人气:3505
火红大气模板之shopex免费
人气:3884
shopex麦考林m18模板
人气:330
Prestashop外贸数码电子商城
人气:263
ECSHOP仿海尔商城电器模板
人气:1036