# coding=utf-8
import MySQLdb
import csv
import random
import pymysql
import pandas as pd
import MySQLdb as mdb
import sys
import time
import os
import numpy as np
import time
import gzip
import datetime
class BlobDataTestor:
def __init__ (self):
self.conn = MySQLdb.connect(host='***',user='***',passwd='***',db='***')
def __del__ (self):
try:
self.conn.close()
except :
pass
def closedb(self):
self.conn.close()
def setup(self):
cursor = self.conn.cursor()
def testGetuserid(self , p_remark):
#cursor = self.conn.cursor()
sql = "select tablename from PATIENT where id='%s'" % (p_remark)
print(sql)
cursor = self.conn.cursor()
cursor.execute( sql)
data=cursor.fetchall()
print("-------------------",data[0][0])
return data[0][0]
def main():
test = BlobDataTestor()
test.setup()
test.testGetuserid("lw1")
test.closedb()
if __name__ == "__main__":
print("start")
print(sys.argv)
main()
python连接mysql
于 2022-04-18 11:33:06 首次发布
该博客主要展示了如何使用Python的MySQLdb和pymysql库连接并操作MySQL数据库,包括建立连接、执行SQL语句(如查询)、关闭连接等基本步骤。示例中涉及了查询PATIENT表中特定ID的tablename。
245

被折叠的 条评论
为什么被折叠?



