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

Python连接SQLServer2000或连接mysql代码示例

时间:2019-10-04 13:20:29      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:com   lse   oca   tuple   fail   cut   mysq   server   游标   

1、Python连接SQLServer2000

# 获取连接
import pymssql
db = pymssql.connect(127.0.0.1, sa, ddh123, "aaa")

# 获取游标,相当于java中的Statement
cursor = db.cursor()

# 执行sql
sql = ‘‘‘
    insert into t_user 
      (username,password,age,height)
    values 
      (‘jlw‘, ‘23333‘, 31, 172)  
‘‘‘
try:
   cursor = db.cursor()
   cursor.execute(sql)
   print(Successfu_sqlserver)
   db.commit()
except:
   print(Failed_sqlserver)
   db.rollback()

cursor.close()
db.close()

2、Python连接MYSQL

import pymysql
db = pymysql.connect(host=localhost,user=root, password=123, port=3306, db=aaa)
data = {
    username: abc333d,
    password: 123456,
    age: 21,
    height:176
}
table = t_user
keys = , .join(data.keys())
values = , .join([%s] * len(data))

sql = INSERT INTO {table}({keys}) VALUES ({values}).format(table=table, keys=keys, values=values)
try:
   cursor = db.cursor()
   cursor.execute(sql, tuple(data.values()))
   print(Successful)
   db.commit()
except:
   print(Failed)
   db.rollback()
cursor.close()
db.close()

 

Python连接SQLServer2000或连接mysql代码示例

标签:com   lse   oca   tuple   fail   cut   mysq   server   游标   

原文地址:https://www.cnblogs.com/hzjdpawn/p/11621760.html

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