At our company we use an SQLite Database to manage our Drawing IDs (CAD Drawings). The code is written in Python and I need to update it. I think the code I have has some problems, but I am quite unsure so I hope to get feedback and criticism from you all!
1. What my code does:
- It saves the ID, Title and Project Number of the Drawing in a SQLite Database
- Therefore it has a function
GetIDto get the latest ID and create a new one from it
Our Drawing IDs look like this "DR0000001". So I need to split the string, convert it to an Integer and add +1. Then rebuild the ID and return it to the OnSave function.
Then if false is not returned I start the save process.
2. Problems I think I have with this code:
- I think the time between getting the recent ID and saving the new one to the DB is susceptible to errors like double usage of IDs.
- I really think this isn't a professional way to do this and I want to learn the correct way.
3. Here is the Code we used up to this point:
def GetID(self):
try:
conn = sqlite3.connect(SQL_PATH)
c = conn.cursor()
c.execute("SELECT DrawingID FROM Drawing LIMIT 1 OFFSET (SELECT COUNT(*) FROM Drawing)-1")
recent_drawing_id = c.fetchall()
new_drawing_id = "DR"+str(int(recent_drawing_id[0][0][2:])+1).zfill(len(recent_drawing_id[0][0][2:]))
return new_drawing_id
except:
wx.MessageBox("Couldn't get new Drawing ID!", "Info", wx.OK | wx.ICON_INFORMATION)
return False
def OnSave(self, event, mode):
drawingID = self.GetID()
if drawingID != False:
conn = sqlite3.connect(SQL_PATH)
c = conn.cursor()
titel = self.tc2.GetValue()
project_number = self.tc3.GetValue()
try:
c.execute("INSERT into Drawing (DrawingID,Titel,ProjectNumber) values (?,?,?)", (userID,titel,project_number))
conn.commit()
c.close()
return
except:
wx.MessageBox("Couldn't create new Drawing entry!", "Info", wx.OK | wx.ICON_INFORMATION)
return
else:
return

