記住一開始一定要安裝 pip install pymysql
import pymysql
def insert_Np_ngram(table_Name,a,b,c,d,e,f):
#localhost,user,password,dbName
db = pymysql.connect("127.0.0.1","test","test","test" )
cursor = db.cursor()
#sql版本
cursor.execute("SELECT VERSION()")
data_db = cursor.fetchone()
#{}代表tablename是變數 因此在結尾的地方要加入 .format(table_Name)
#有幾個欄位就有幾個values %s(字串) %d(整數) 記得value 後要加入 data = (a,b,c,d,e,f)
sql = """INSERT INTO {}(a,b,c,d,e,f)
VALUES (%s,%s,%s,%s,%s,%s) """.format(table_Name) # %s字串 %d整數十進位
data = (a,b,c,d,e,f)
cursor.execute(sql, data) #變數插入
db.commit()
def select_Db_ngram_time(user,table_Name,date):
db = pymysql.connect("127.0.0.1","test","test","test",charset='utf8')
cursor = db.cursor()
cursor.execute("SELECT VERSION()")
cur = db . cursor ()
#select 的寫法也大同小異 只是 where的值 WHERE a = '%s' 後 要在sql指令最後加入 % (變數)
cur . execute ( """SELECT * FROM {} WHERE a = '%s' AND b = '%s' """.format(table_Name) % (case_user,dateTime))
results = cur.fetchall ()
return results
db.commit()
def update_deal_History(col1,companyId,saveId):
#update的寫法也大同小異
db = pymysql.connect("127.0.0.1","test","test","test",charset = 'utf8')
cursor = db.cursor()
sql = """UPDATE dealHistory SET Stage = '%s' WHERE Company_Id = '%s' And Id = '%s' """%(col1,companyId,saveId)
cursor.execute(sql)
db.commit()
留言列表