码迷,mamicode.com
首页 > 数据库 > 详细

pymysql数据库操作

时间:2017-07-01 23:22:10      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:inf   sys.path   weight   游标   insert   cut   ret   pat   修改   

首先要pip install pymysql

import pymysql,sys
#print(sys.path)
from settings import mysql_info
# sql=‘insert into user (username,password) VALUE ("haha","man")‘
# sql2=‘select * from user limit 3‘
def OpMysql(host,port,user,passwd,db,charset,sql):
conn = pymysql.connect(host=host, port=port, user=user, passwd=passwd, db=db,charset=charset)
# 执行SQL,并返回收影响行数
#effect_row = cursor.execute("update students set name = ‘niuhy‘ where id = 1;")
# 创建游标
cur = conn.cursor(cursor=pymysql.cursors.DictCursor)#返回字典
#cur = conn.cursor()#返回元组tuple,元组不可修改
cur.execute(sql)
if sql.startswith(‘select‘):#判断是什么语句
res = cur.fetchall()
else:
conn.commit()
res = 88
cur.close()
conn.close()
return res

def jude_username(username):
pass

#@judge_username
def login():
username = input(‘set your username: ‘)
pass

#OpMysql(mysql_info[‘host‘],mysql_info[‘port‘],mysql_info[‘user‘],mysql_info[‘passwd‘],mysql_info[‘db‘],mysql_info[‘charset‘],sql2)
#if __name__==‘__main__‘:

pymysql数据库操作

标签:inf   sys.path   weight   游标   insert   cut   ret   pat   修改   

原文地址:http://www.cnblogs.com/yuer011/p/7103716.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!