首页 > 数据库技术 > 详细

操作数据库

时间:2019-11-01 00:05:23      阅读:95      评论:0      收藏:0      [点我收藏+]

import pymysql
ip ="1.1.1.1"
user = ‘j‘
password="123456"
db=‘j‘
port=3306
charset=‘utf8‘
conn = pymysql.connect(host=ip,user=user,password=password,
db=db,port=port,charset=charset,autocommit=True)#建立连接

cur = conn.cursor(pymysql.cursors.DictCursor) #游标

sql = ‘select * from app_myuser limit 5;‘

cur.execute(sql)#执行sql语句,insert 、update 、delete
#conn.commit() #提交
all = cur.fetchall()
# one = cur.fetchone()
# many = cur.fetchmany(2)
cur.close()
conn.close()

# print(one)
# print(many)
print(all)


def op_mysql(sql):
db_info = {‘user‘: ‘jxz‘, ‘password‘: ‘123456‘,
‘host‘: ‘118.24.3.40‘, ‘db‘: ‘jxz‘, ‘port‘: 3306, ‘charset‘: ‘utf8‘,
‘autocommit‘: True}
conn = pymysql.connect(**db_info) # 建立连接
cur = conn.cursor(pymysql.cursors.DictCursor) # 游标
cur.execute(sql) # 执行sql语句,insert 、update 、delete
result = cur.fetchall()
cur.close()
conn.close()
return result

 

op_mysql(‘sdfsdf‘) #user=j,password=123456

 

"""
create table mjz (id int unique not null, name varchar(20) not null, phone varchar(11) unique not null);
insert into mjz (id,name,phone) values (1,"小白","19812343211");

insert into mjz (id,name,phone) values (2,"小白2","17812343211");
update mjz set name="春光" where id =1;
delete from mjz where id=3;
"""

操作数据库

原文:https://www.cnblogs.com/lapt/p/11774622.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!