Sqlite insert not working with python -


i'm working sqlite3 on python 2.7 , facing problem many-to-many relationship. have table fetching primary key this

current.execute("select extensionid tblextensionlookup extensionname = ?",[ext]) 

and fetching primary key table

current.execute("select hostid tblhostlookup hostname = ?",[host]) 

now doing have third table these 2 keys foreign keys , inserted them this

current.execute("insert tblextensionhistory values(?,?)",[hid,eid]) 

the problem don't know why last insertion not working keeps giving errors. have tried is:

first thought because have autoincrement primary id last mapping table didn't provide, isn't supposed consider it's auto incremented? went ahead , tried adding null,none,0 nothing works.

secondly thought maybe because i'm not getting values tables above tried printing out , shows works.

any suggestions doing wrong here?

edit : when don't provide primary key error as

the table has 3 columns provided 2 values 

and when provide them none,null or 0 says

parameter 0 not supported because of unsupported type 

i tried implementing @abarnet way still keeps saying parameter 0 not supported

connection = sqlite3.connect('webinfrastructurescan.db')     connection:         current = connection.cursor()         current.execute("select extensionid tblextensionlookup extensionname = ?",[ext])         eid = current.fetchone()         print eid         current.execute("select hostid tblhostlookup hostname = ?",[host])         hid = current.fetchone()         print hid          current.execute("insert tblextensionhistory(hostid,extensionid) values(?,?)",[hid,eid]) 

edit 2 :

the database schema :

table 1:

create table tblhostlookup (  hostid integer primary key autoincrement,  hostname text); 

table2:

 create table tblextensionlookup (   extensionid integer primary key autoincrement,   extensionname text); 

table3:

create table tblextensionhistory ( extensionhistoryid integer primary key autoincrement, hostid integer, foreign key(hostid) references tblhostlookup(hostid), extensionid integer, foreign key(extensionid) references tblextensionlookup(extensionid)); 

it's hard sure without full details, think can guess problem.

if use insert statement without column names, values must match columns given in schema. can't skip on of them.*

the right way fix use column names in insert statement. like:

current.execute("insert tblextensionhistory (hostid, extensionid) values (?,?)",                 [hid, eid]) 

now can skip columns want (as long they're autoincrement, nullable, or otherwise skippable, of course), or provide them in order want.


for second problem, you're trying pass in rows if single values. can't that. code:

eid = current.fetchone() 

this return like:

[3] 

and try bind extensionid column, gives error.


in future, may want try write , debug sql statements in sqlite3 command-line tool and/or favorite gui database manager (there's simple extension runs in firefox if don't want fancy) , them right, before try getting python right.


* not true databases. example, in msjet/access, must skip on autoincrement columns. see sqlite documentation how sqlite interprets insert no column names, or similar documentation other databases.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -