samedi 14 mars 2015

Where does my leading zeroes go?

I am reading in numbers from a comma seperated textfiles, where some numbers are on the form 000000084. These files are added to a messageclass, and then put in a SQLite database. The problem I have is that SQLite saves these 000000084 as 84, even though I create the table column as a string.


Inputfile



something,000000018,213123
somethingelse,000000025,213123


Creation of database:



def createDatabase(databasepath):
con = lite.connect(databasepath)
with con:
cur = con.cursor()
cur.execute("CREATE TABLE staticMessage(userid string)")


Message class:



class StaticMessage:
def __init__ (self, userid):
self.userid = userid


Extracting message from file:



def extractStaticMessages(filepath):
global staticMessage
staticMessage = []
lengths = []
f = open(filepath, 'r')
for line in f:
newline = line.split(",")
message = StaticMessage(newline[0])
staticMessage.append(message)


Writing message to database:



def writeStaticToDatabase(databasepath):
con = lite.connect(databasepath)
con.isolation_level = None
with con:
cur = con.cursor()
cur.execute('BEGIN TRANSACTION')
for i in range(0, len(staticMessage)):
cur.execute("INSERT INTO staticMessage(userid) VALUES(?)",
(staticMessage[i].userid)
cur.execute('COMMIT')


When I then query my database with f.ex:



select userid from staticMessage where userid='000000084'


I get 84


This is a major annoyance for me, because I want to query my database to get the number of userids less than nine digits.


TL/DR: where does my leading zeros go when inserting into SQLite?


Aucun commentaire:

Enregistrer un commentaire