I have an SQLite database with a column whose entries are a string of the following form "A-B-C" where A, B, C have variable length and are delimited by the "-" character.
In particular, A should be 1 or 2 characters, B 1-4 characters and C 1-5 characters but they may be even longer for some unknown reason.
I tried to split these characters the following way:
- A column where I would have the A substring
- Another one where I'd have the B substring
- Last one where I'd have the C substring.
However, since SQLite doesn't support string splitting and my efforts at hacking with substr()
and different positioning ended up with a lot of confusion.
I'm trying to do this in python now. However I'm quite new to this, and while I was able to connect to the database and commit statements this way, I'm not quite sure how to go about doing what I want. I saw the str.rsplit()
function but I'm not exactly sure how to mix between both languages.
For instance, I should probably write
c = conn.execute(
UPDATE database SET Column_1 =
def First(
str.rsplit(Column), 1)
SELECT Column FROM database )
with the First
function returning the first value of the split string from Column
, and so on for Column_2
and Column_3
.
Once I have that I need to format each of column_1, column_2 and column_3 so that they each have at least 2, 4 and 5 characters each by adjoining zeroes to the left of the characters in the columns, depending on the length of the resulting string.
EDIT:
I'm having quite a few problems updating the database:
The following yields the first value:
with con: cur = con.cursor() cur.execute('SELECT Column1 FROM MainTable') row = cur.fetchone() for line in row: a, b, c= line.split('-') print (b);
A slight modification using:
while True:
cur.fetchone()== None then Break
yields all values. However, I can get it to update the database for a given column say Col2, because it has a ton of syntax errors related to mixing python and SQL.