Python连接PostgreSQL
import psycopg2 //引入pgsql的包
//连接信息
conn = psycopg2.connect(host='127.0.0.1',
port='5432',
database='postgres',
user='postgres',
password='123456')
cursor = conn.cursor() // 定义游标
cursor.execute('select * from table ')
result = cursor.fetchall() //得到查询数据(二维数组)
print(result)
print(result[1])
print(result[0][1])
data=[(9999999,result[0][1],'1236456'),(98989898,result[1][1],'123456')] //定义二维数组
try:
cursor.executemany('insert into table (id,name,card) values( %s,%s,%s)' ,data ) // 写入数据
conn.commit()
print('成功插入'+str(len(data))+'条数据')
except Exception as e :
conn.rollback();
print('失败失败失败::'+str(e))
cursor.close() //关闭游标
conn.close() // 关闭连接
Python连接SqlServer
import pymssql
conn = pymssql.connect(host='127.0.0.1',
port= 1433,
database='master',
user='sa',
password='123456')
cursor = conn.cursor()
cursor.execute('select * from table ')
result = cursor.fetchall()
print(result)
cursor.close()
conn.close()
Python连接MySQL
import pymysql
conn2 = pymysql.connect(host='127.0.0.1',
port= 3306,
user='root',
passwd='123456',
db='mysql')
cursor2 = conn2.cursor()
cursor2.execute('SELECT * FROM table')
result2 = cursor2.fetchall();
print(result2)
cursor2.close()
conn2.close()