python 连接三大数据库

python 连接三大数据库,第1张

python 连接三大数据库
  1. mysql 数据库连接

import pymysql

conn = pymysql.connect(host=127.0.0.1, port=3306, user=root, password=1234, db=数据库名称, autocommit=True)

cursor = conn.cursor()

# 添加语句
sql = ‘’‘insert into Z_SQL (record, title, sqldata) values ("%s","%s","%s") ‘’’ % (insert_record, inputtext, inputsqldata)
cursor.execute(sql)

# 删除语句
sql = 'DELETe FROM Z_SQL WHERe record =%s ’
cursor.execute(sql, delete_no)

# 查找
sql = ‘SELECT* FROM Z_SQL’
cursor.execute(sql)
# 处理结果集
for line in cursor.fetchall():
print(line)

# 更新
sql = ‘’‘update Z_SQL set title ="%s",sqldata ="%s" where record ="%s" ‘’’ % (update_title, update_sqldata, update_no)
cursor.execute(sql)

  1. Oracle 数据库连接

import cx_Oracle

conn=cx_Oracle.connect(user,password,ip+":"+port)
cursor=conn.cursor()

# 删除语句
sql = f’’‘DELETE FROM Z_SQL WHERe record =’{delete_no}’ ‘’’
cursor.execute(sql)

# 添加语句
sql = f’’’ INSERT INTO Z_SQL(record, title, sqldata) values (’{insert_record}’,’{inputtext}’,’{inputsqldata} ‘) ‘’’
cursor.execute(sql)

conn.commit()

# 更新
sql = f’’‘update Z_SQL set title =’ {update_title} ‘, sqldata =’ {update_sqldata} ’ where record =’{update_no}’ ‘’’
cursor.execute(sql)
conn.commit()

# 查找
sql = ‘SELECT* FROM Z_SQL’
cursor.execute(sql)
# 处理结果集
for line in cursor.fetchall():

  1. Sqlserver 数据库连接

mport pymssql

conn=pymssql.connect(ip,user,password,db,autocommit=True)
cursor=conn.cursor()

# 添加语句
sql =’’‘insert into Z_SQL (record, title, sqldata) values (’%s’,’%s’,’%s’) ‘’’ % (insert_record, inputtext, inputsqldata)
cursor.execute(sql)

# 删除语句
sql = 'DELETe FROM Z_SQL WHERe record =%s ’
cursor.execute(sql, delete_no)

# 更新
sql = ‘’‘update Z_SQL set title =’%s’,sqldata =’%s’ where record =’%s’ ‘’’ % (update_title, update_sqldata, update_no)
cursor.execute(sql)

# 查找
sql = ‘SELECT* FROM Z_SQL’
cursor.execute(sql)

# 处理结果集
for line in cursor.fetchall():
print(line)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5157216.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存