这是我在这里的第一篇文章,我在这里找到了很多答案,我非常有信心,有些可以帮助我。因为我现在使用 Python 才半年左右,这可能是一个非常愚蠢的初学者问题。原谅我到目前为止...
在我的项目中,我有几个不同的功能。下面列出了其中的两个。mysqlLogbookIndex是一个线程,它显示使用数据库中某些表的名称刷新列表。目前与数据库的连接已经存在,该函数被调用,到目前为止它工作正常。最后我可以看到一个包含所有表名的元组。
但是现在第二个名为create_flight的函数进来了。它是 Tkinter 按钮的回调,并在我的数据库中创建了一些新表。它使用与mysqlLogbookIndex相同的预打开连接。
我希望在mysqlLogbookIndex的下一个周期中看到新表是我的元组,但发生的事情是结果变成了 None。
你知道为什么吗?
小部件功能:
def create_flight(self):
# *********************************************************************************************
# * Create new flight table *
# *********************************************************************************************
if self.sql_write.get():
# ********************************************
# * Try to connect message *
# ********************************************
self.printLog(self.lbl_sql_write, self.LANG['tryCreateTable'], 'normal')
logging.info('Creating table for flight tracking...')
# ********************************************
# * Create name tables *
# ********************************************
event = self.oprSett['mysql']['event']
now = str(int(time.time()))
mainName = "tbl_%s_%s_" % (event, now)
trackingTable = mainName + "flighttrack"
logging.debug('Name of tracking table: %s', trackingTable)
unitsTable = mainName + "units"
logging.debug('Name of units table: %s', unitsTable)
headerTable = mainName + "header"
logging.debug('Name of header table: %s', headerTable)
# ********************************************
# * Read SQL parameter *
# ********************************************
logging.debug('Reading CSV file for table structure...')
csvFile = "config/newFlight.csv"
try:
sqlCsv = csv.reader(open(csvFile, 'rb'),
delimiter = ',',
quotechar = '"',
quoting = csv.QUOTE_ALL
)
except:
msg = csvFile
msg += "\n\n"
msg += self.LANG['e13']
tkMessageBox.showerror("Error 13", msg)
self.printLog(self.lbl_sql_write, self.LANG['e13'], 'error')
logging.error('File not found!')
#print "[Error 13] " + self.LANG['e13']
return 0
# Transfer data from CSV file into own list
sqlVars = []
for row in sqlCsv:
if len(row) == 4 and row[0][0] != "#": # No comment
sqlVars.append(row)
# *************************************************
# * Create SQL statement to create tracking table *
# *************************************************
# Head for creating new table
sql = "CREATE TABLE IF NOT EXISTS `%s` (\n" % trackingTable
sql += " `ID` int(11) NOT NULL AUTO_INCREMENT,\n" # Becomes primary key
# Parse SQL variables from CSV file
for row in sqlVars:
if len(row[2]) > 0: # Data type requires length
sql += " `%s` %s(%s) NOT NULL COMMENT '%s',\n" % (row[0], row[1], row[2], row[3])
else: # Data type not requires length
sql += " `%s` %s NOT NULL COMMENT '%s',\n" % (row[0], row[1], row[3])
# Footer of SQL statement for creating new table
sql += " PRIMARY KEY (`ID`)\n"
sql += ") ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci AUTO_INCREMENT=0;\n"
sql += "\n"
# In debug mode print SQL statement to console
#logging.debug('SQL statement to create tracking table:\n%s', sql)
# **********************************************
# * Create SQL statement to create units table *
# **********************************************
# Head for creating new table
sql += "CREATE TABLE IF NOT EXISTS `%s` (\n" % unitsTable
sql += " `ID` int(11) NOT NULL AUTO_INCREMENT,\n" # Becomes primary key
sql += " `Dataref` varchar(10) COLLATE latin1_german1_ci NOT NULL,\n"
sql += " `Unit` varchar(10) COLLATE latin1_german1_ci NOT NULL,\n"
sql += " PRIMARY KEY (`ID`)\n"
sql += ") ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci AUTO_INCREMENT=1 ;\n"
sql += "\n"
# Parse SQL variables from CSV file
for row in sqlVars:
# Insert units in tbl_units
sql += "INSERT INTO %s VALUES ('', '%s', '%s');\n" % (unitsTable, row[0], row[3])
sql += "\n"
# In debug mode print SQL statement to console
#logging.debug('SQL statement to create units table:\n%s', sql)
# ***********************************************
# * Create SQL statement to create header table *
# ***********************************************
# Head for creating new table
sql += "CREATE TABLE IF NOT EXISTS `%s` (\n" % headerTable
#sql += " `ID` int(11) NOT NULL AUTO_INCREMENT,\n" # Becomes primary key
sql += " `Parameter` char(21) COLLATE latin1_german1_ci NOT NULL,\n"
sql += " `Value` varchar(100) COLLATE latin1_german1_ci NOT NULL,\n"
sql += " PRIMARY KEY (`Parameter`)\n"
sql += ") ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_german1_ci AUTO_INCREMENT=1 ;\n"
sql += "\n"
# IGC syntax from: http://carrier.csi.cam.ac.uk/forsterlewis/soaring/igc_file_format/igc_format_2008.html
# Adding header parameters, some values are coming later
sql += "INSERT INTO %s VALUES ('AXXX001', '');\n" % (headerTable) # Manufacturer code
sql += "INSERT INTO %s VALUES ('HFFXA', '035');\n" % (headerTable) # Fix accuracy
sql += "INSERT INTO %s VALUES ('HFDTE', '');\n" % (headerTable) # UTC date of flight
sql += "INSERT INTO %s VALUES ('HFPLTPILOT', '');\n" % (headerTable) # Pilots name
sql += "INSERT INTO %s VALUES ('HFGTYGLIDERTYPE', 'KA8B');\n" % (headerTable) # Glider type
sql += "INSERT INTO %s VALUES ('HFGIDGLIDERID', 'D1389');\n" % (headerTable) # Glider callsign
sql += "INSERT INTO %s VALUES ('HFDTM100DATUM', 'WGS-1984');\n" % (headerTable) # GPS datum
sql += "INSERT INTO %s VALUES ('HFGPSGPS', 'X-PLANE 10');\n" % (headerTable) # Manufacturer of GPS module
sql += "INSERT INTO %s VALUES ('HFFTYFRTYPE', 'FLORIANMEISSNER,HCM');\n" % (headerTable) # Logger type
sql += "INSERT INTO %s VALUES ('HFRFWFIRMWAREVERSION', '%s');\n" % (headerTable, self.VERSION) # Firmware version
sql += "INSERT INTO %s VALUES ('HFRHWHARDWAREVERSION', '%s');\n" % (headerTable, self.XPLANEVERSION) # Hardware version
sql += "INSERT INTO %s VALUES ('HFCCLCOMPETITIONCLASS', 'CLUB');\n" % (headerTable) # Competition class
# ********************************************
# * Handover SQL statement to create table *
# * to DB. *
# ********************************************
logging.debug('SQL statement to create all tables:\n%s', sql)
try:
cur = self.conn.cursor()
cur.execute(sql)
cur.close()
except pymysql.Error, e:
tkMessageBox.showerror("Error 9", self.LANG['e9'])
self.printLog(self.lbl_sql_write, self.LANG['e9'], 'error')
#print "[Error 9] " + self.LANG['e9']
logging.error('Could not create tracking table!')
logging.debug(e)
return 0
# ********************************************
# * Print success message *
# ********************************************
logging.info('Tracking table created.')
self.printLog(self.lbl_sql_write, self.LANG['doneCreateTable'], 'success')
text = self.LANG['flightId'] + " " + trackingTable
self.printLog(self.lbl_sql_write, text, 'normal', timestamp=False)
# Make trackingTable public
self.tableName = trackingTable
# Enable tambour register to write in database
self.tambourInMysql = True
刷新循环:
def mysqlLogbookIndex(self):
delay = float(self.oprSett['logbook']['refresh_delay']) / 1000
# ********************************************************************
# * Run only if MySQL connection from writeMysql.py is active *
# ********************************************************************
while self.sql_write.get():
# ********************************************
# * Query to get list with tables *
# ********************************************
logging.info('Querying list with tables from database')
dbName = self.oprSett['mysql']['db']
sql = "SELECT TABLE_NAME\n"
sql += "FROM `information_schema`.`TABLES`\n"
sql += "WHERE `TABLE_SCHEMA` LIKE '%s'" % dbName
try:
cur = self.conn.cursor()
cur.execute(sql)
result = cur.fetchall()
cur.close()
except pymysql.Error, e:
logging.warning('Could not take flights from database!')
logging.debug(e)
self.printLog(self.lbl_sql_write, self.LANG['e20'], 'error')
#continue
else:
logging.info('Logbook refreshed.')
print result
在此先感谢所有有用的帖子...