这俩天在学习PYTHON操作数据库的知识。其实基本SQL命令是与以前学习的MYSQL命令一致,只是增加了一些PYTHON语句。
1,安装pymysql,并导入。
import pymysql
2,因为用的就python2,所以加上一步。
import sys
reload(sys)
sys.setdefaultencoding('gb18030') 3,因为我已经在本地下载了mysql数据库,并新建了表。
所以这一步,我就直接连接本地的数据库就可以了。
conn=pymysql.connect(host='127.0.0.1',user='root',password='123456',db='caipiao') ###这里我是连接的本地数据库,没有写端口号3306,数据库名称:caipiao,用户是root,
cur=conn.cursor()
###这里就是我们要进行的操作步骤,先空着。####
conn.commit()
cur.close() ##################################################下面来讲一下常用的SQL命令#####################################
1,插入数据:(表名也是:caipiao)
conn=pymysql.connect(host='127.0.0.1',user='root',password='123456',db='caipiao')
cur=conn.cursor()
re=cur.execute("insert into caipiao values(1,'baozi',1,1),(2,'mantou',2,2),(3,'youtiao',3,3)")
conn.commit()
cur.close()
conn.close()
print (re)
现在我们可以访问我们本地的数据库,可以发现我们已经成功插入了三条数据。(id=1,name='baozi',touzhu=1,yingli=1)(*******)(******) 2,查询数据:
conn=pymysql.connect(host='127.0.0.1',user='root',password='123456',db='caipiao')
cur=conn.cursor()
cur.execute("select* from caipiao")
ret1=cur.fetchall() ####把查询到的数据都显示出来。
print (ret1) ret2=cur.fetchmany(3) ###获取表中前3条数据并打印出来
print (ret2) ret3=cur.fetchone() ###获取表中第一行数据并打印出来
print (ret3) conn.commit()
cur.close()
conn.close() 3,删除数据
conn=pymysql.connect(host='127.0.0.1',user='root',password='123456',db='caipiao')
cur=conn.cursor()
cur.execute("delete from caipiao where id=1")
conn.commit()
cur.close()
conn.close() 4,更改数据
conn=pymysql.connect(host='127.0.0.1',user='root',password='123456',db='caipiao')
cur=conn.cursor()
cur.execute("update caipiao set name='doujiang' where id=2")
conn.commit()
cur.close()
conn.close() 5,fetch数据类型 (关于默认获取的数据是元祖类型,如果想要字典类型的数据)
conn=pymysql.connect(host='127.0.0.1',user='root',password='123456',db='caipiao')
cur=conn.cursor(cursor=pymysql.cursors.DictCursor)
cur.execute("select* from caipiao")
re=cur.fetchall()
print (re)
打印出来就是:###
[{u'touzhu': '10', u'caizhong': 'chongqingshishicai', u'yingli': '10', u'id': 3, u'name': 'baozi'}, {u'touzhu': '10', u'caizhong': 'chongqingxingyun', u'yingli': '20', u'id': 4, u'name': 'youtiao'}, {u'touzhu': '10', u'caizhong': '*shishicai', u'yingli': '30', u'id': 5, u'name': 'mayuan'}, {u'touzhu': '10', u'caizhong': 'dafashishicai', u'yingli': '40', u'id': 6, u'name': 'mianbao'}, {u'touzhu': '10', u'caizhong': 'jilinkuaisan', u'yingli': '50', u'id': 7, u'name': 'changfen'}, {u'touzhu': '10', u'caizhong': 'anhuikuaisan', u'yingli': '60', u'id': 8, u'name': 'bingan'}, {u'touzhu': '10', u'caizhong': 'dafashishicai', u'yingli': '50', u'id': 9, u'name': 'mianbao'}, {u'touzhu': '10', u'caizhong': 'beijingsaiche', u'yingli': '20', u'id': 2, u'name': 'baozi'}, {u'touzhu': '11', u'caizhong': 'chongqingshishicai', u'yingli': '11', u'id': 7, u'name': 'yiyi'}, {u'touzhu': '12', u'caizhong': 'dafakuaisan', u'yingli': '12', u'id': 8, u'name': 'xiha'}, {u'touzhu': '11', u'caizhong': 'dafa1', u'yingli': '12', u'id': 8, u'name': 'man1'}, {u'touzhu': '12', u'caizhong': 'dafa2', u'yingli': '13', u'id': 9, u'name': 'man2'}, {u'touzhu': '11', u'caizhong': 'dafa1', u'yingli': '12', u'id': 8, u'name': 'man1'}, {u'touzhu': '12', u'caizhong': 'dafa2', u'yingli': '13', u'id': 9, u'name': 'man2'}, {u'touzhu': '11', u'caizhong': 'dafa1', u'yingli': '12', u'id': 8, u'name': 'man1'}, {u'touzhu': '12', u'caizhong': 'dafa2', u'yingli': '13', u'id': 9, u'name': 'man2'}, {u'touzhu': '11', u'caizhong': 'dafa1', u'yingli': '12', u'id': 8, u'name': 'man1'}, {u'touzhu': '12', u'caizhong': 'dafa2', u'yingli': '13', u'id': 9, u'name': 'man2'}, {u'touzhu': '11', u'caizhong': 'dafa1', u'yingli': '12', u'id': 8, u'name': 'man1'}, {u'touzhu': '12', u'caizhong': 'dafa2', u'yingli': '13', u'id': 9, u'name': 'man2'}]
######
conn.commit()
cur.close()
conn.close()