mysql与pymysql

import pymysql

db = pymysql.connect(host= ‘localhost’ , port = 3306,user = ‘root’ , password = ‘’, database = ‘pachong’ , charset = ‘utf8’)

#获取会话指针,用来调用sql语句

#插入数据

sql = ‘INSERT INTO test(company ,title,href,date,source) VALUES(%s,%s,%s,%s,%s)’

cur.execute(sql , (company,title,href,date,source))

#查找数据

sql1 = ‘SELECT * FROM test WHERE company = %s’
cur.execute(sql1 , company)

data = cur.fetchall()

cur.fetchall()

删除数据

sql2 = ‘DELETE FROM test WHERE company = %s’

DELETE FROM test WHERE company = %s

cur.execute(sql2 , company)

db.commit()#更新数据表

cur.close() # 关闭会话指针

db.close() # 关闭数据库连接

你可能感兴趣的:(金融计算与编程)