Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SqLite near field error
#1
I wrote this code. When I run it I don't get errors but I can't get the values inserted in the table, I end up with a database with table1 that's empty. Any help?

TabelName = 'table1'
values = [1, 1, 1.0, 1.0, 0]
columnsNmes =['Day', 'Month', 'Hour', 'HOY', 'Sun Falling on Tower Facade']
fieldTypes = [REAL,REAL,REAL,REAL,REAL]
filepath = 'C:\xampp\htdocs\FT-test9.db'
conn = sqlite3.connect(filePath)

c = conn.cursor()
try:
    c.execute('CREATE TABLE IF NOT EXISTS {tn} ({nf} {ft})'\
                .format(tn=tableName, nf=columnsNames[0], ft=fieldTypes[0]))
        
       
    for i, j in zip(columnsNames[1:], fieldTypes[1:]):
        c.execute("ALTER TABLE {tn} ADD COLUMN '{cn}' {ct}"\
                    .format(tn=tableName, cn=i, ct=j))
    
    except:
        pass

newColumnsNmes = []

for d in data:
    m =float(d)
    newColumnsNames.append(m)
        
for m,n in zip(newColumnsNames, data):     
    c.execute("INSERT INTO {tn} {cn} VALUES {vn}"\
        .format(tn=tableName, cn=m, vn= n))
conn.commit()
conn.close()
Reply
#2
how do you know you're not getting errors when your except clause is ignoring any errors?

replace the pass (temporarilly ... replace exception with actual exception later) with:
#at start or program
import sys
# ...
#Replace pass with:
print("Unexpected error:", sys.exc_info()[0])
This will show all errors.
Reply
#3
(May-24-2018, 02:39 PM)Larz60+ Wrote: how do you know you're not getting errors when your except clause is ignoring any errors?
The part of the code that has the "except" clause gets created successfully, I get a table with the right structure
Reply
#4
Ok, Add the print statement here:
for m,n in zip(newColumnsNames, data):
    sqlstr = "INSERT INTO {tn} {cn} VALUES {vn}".format(tn=tableName, cn=m, vn= n))
    print('sqlstr: {}'.format(sqlstr))
    c.execute(sqlstr)
#    c.execute("INSERT INTO {tn} {cn} VALUES {vn}"\
#        .format(tn=tableName, cn=m, vn= n))
Reply
#5
(May-24-2018, 02:46 PM)Larz60+ Wrote: Ok, Add the print statement here:
for m,n in zip(newColumnsNames, data):
    sqlstr = "INSERT INTO {tn} {cn} VALUES {vn}".format(tn=tableName, cn=m, vn= n))
    print('sqlstr: {}'.format(sqlstr))
    c.execute(sqlstr)
#    c.execute("INSERT INTO {tn} {cn} VALUES {vn}"\
#        .format(tn=tableName, cn=m, vn= n))

Aha, that's what I got, but I still don't understand what the problem is:

Runtime error (SyntaxErrorException): unexpected token ')'
File "", line 32
sqlstr = "INSERT INTO {tn} {cn} VALUES {vn}".format(tn=tableName, cn=m, vn= n))

^
SyntaxError: unexpected token ')'
Reply
#6
add print to see what values of m, n, tn are

I had an extra close parentheses

also try:
sqlstr = "INSERT INTO {} {} VALUES {}".format(tableName, m, n)
Reply
#7
(May-24-2018, 03:00 PM)Larz60+ Wrote: add print to see what values of m, n, tn are

I had an extra close parentheses

also, try:
sqlstr = "INSERT INTO {} {} VALUES {}".format(tableName, m, n)
I tried both suggestions and I don't get errors and also the values don't get inserted, but I figured out that something is odd in the iteration process because when I run this:
for m,n in zip(newColumnsNames, data):
    print m, n 
I don't get anything printed, however when I run this:
for m,n in zip(newColumnsNames, data):
    pass
print m, n 
I get the correct values of m and n printed
It's worth mentioning that I use IronPython inside a 3D graphics application environment

Also, I figured out that I accidentally was trying to convert my columns' names into float instead of doing this into to the data(the values). So I corrected this, then I put the print statement that @Larz60+ recommended and this is the error message I got:

sqlstr: INSERT INTO table1 Day VALUES 1.0
Runtime error (Exception): near "Day": syntax error
Traceback:
line 35, in script




Please note Day is a column name
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help With Python SQLite Error Extra 10 15,045 May-04-2022, 11:42 PM
Last Post: Extra
  Error while transferring data from sqlite to elasticsearch - please help! ps96068 1 2,680 Jun-12-2021, 09:24 AM
Last Post: ibreeden
  UPDATE SQLITE TABLE - Copy a fields content to another field. andrewarles 14 4,405 May-08-2021, 04:58 PM
Last Post: ibreeden
  Bug ? when dataclass field name == field type Cyril 0 1,561 Oct-22-2020, 03:26 AM
Last Post: Cyril
  SQLite Unique constraint failed error djwilson0495 3 13,476 Aug-14-2020, 05:23 PM
Last Post: ndc85430
  Error SQLite objects created in a thread can only be used in that same thread. binhduonggttn 3 15,587 Jan-31-2020, 11:08 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020