def store_sql(filepath):
    db=MySQLdb.connet(host="localhost",user="******",passwd="123",db="testdb")
    cursor=db.cursor()          #类似于操作数据库的指针
    cursor.execute("show tables in testdb;")
    tables=cursor.fetchall()
    findtables=False
    for table in tables:
        if 'code' in table:
            findtables=True
            print("table already exits")
    if not findtables:
        cursor.excute("CREAT TABLE code(
        id INT NOT NULL AUTO_INCREMENT,
        code VARCHAR(10)
        PRIMARY KEY(id)
        );"
        )
    f=open(filepath,'rb')
    for line in f.readlines():
        coupon=line.strip()
        cursor.execute("INSERT INTO code VALUES(%s);",[coupon])

    db.commit()
    cursor.close()
    db.close()


    if __name__='__main__':
        store_sql('Running_result.txt')
Beispiel #2
0
#pip install mysql-client
#http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient
#实验python 是否可以连接上数据库
#python命令环境下 打印import MySQLdb
import MySQLdb

try:
	conn = MySQLdb.connet(
		host='127.0.0.1',
		user='******',
		passwd='',
		db='news',
		port=3308,
		charset='utf8'
	)
	#执行
	cursor = conn.cursor()
	cursor.execute('SELECT * FROM 'news' ORDER BY 'created_at' DESC;')
	rest = cursor.fetchone()
	#cursor.fetchall()
	print(rest) 

	#关闭连接
	conn.close() 
except MySQLdb.Error as e:
	print('Error: %s' % e)

import serial
import time
import MySQLdb as mdb

arduino = serial.Serial("/dev/ttyUSB1", 9600)
data = arduino.readline()
pieces = data.split("\t")
temperature = pieces[0]
humidity = pieces[1]
con = mdb.connet('localhost', 'gtr', 'gtrlab', 'gtr_2nd')
while True:
    cursor = con.cursor()
    cursor = execute("""INSERT INTO ITC VALUES(%s,%s,%s)""",
                     ('', temperature, humidity))
    con.commit()
    cursor.close()
    time.sleep(5)