1, Python connection SQLServer2000
# Get connected Import pymssql DB = pymssql.connect ( ' 127.0.0.1 ' , ' SA ' , ' ddh123 ' , " AAA " ) # Get the cursor, the equivalent Statement java Cursor = db.cursor () # execute SQL SQL = '' ' INSERT INTO T_USER (username, password, Age, height) values (' JLW ',' 23333 ', 31 is, 172) ' '' the try : Cursor = db.cursor () Cursor.execute(sql) print('Successfu_sqlserver') db.commit() except: print('Failed_sqlserver') db.rollback() cursor.close() db.close()
2, Python connection 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()