Hey, I'm having trouble dealing with this - I made a table:

create table prodzina(
ID_prac integer references Ppracownicy,
stan_cywilny varchar(20),
ilosc_dzieci integer,
ubezpieczenie bool);

and I got my script <part of it>:

x_find='SELECT * from ppracownicy where nazwisko='+a+nazw+a+' and imie='+a+imie+a+' and id_dzial='+a+iddzial+a+' and pensja='+a+pensja+a+' and stanowisko='+a+stan+a+''
cur.execute(x_find)
rows=cur.fetchall()
idpracownika=""
for row in rows: 
idpracownika=row[0]
if choice=="2":

//and now I'm trying to insert that table with, like so:

x_default='insert into prodzina values ('+idpracownika+','+a+'kawaler'+a+',0,false)'
cur.execute(x_default)

The type of the field I'm refering to in ppracownicy is serial primary key.
Oh, and all those a == ', for example where nazwisko='+'+nazw+'+'

Inserting this way did work for a table with no references to another one, but here I get an error:
Can't concatenate an int object with a str object.

How can I insert values with one of them referring to another table, and the rest of the values inserted as constants?

Recommended Answers

All 3 Replies

One of the values that you are trying to concatenate is an integer. You need to wrap it with str() in order to convert it and concatenate.

Also, I don't see you performing any commit() operations. If you do not commit your transactions they will never be posted to the database.

It looks like your problem is here
'+idpracownika+','+a+'kawaler'+a+',0,false)'
You have +idpracownika
+','
+a
+'kawaler'
+a
+',0,false) ## you have to have at least one more (or one less) single quote here
If you create a string and use that, you can then print the string to see if it is accurate
key_str = 'idpracownika,%skawaler%s,0,false' % (a, a) (not sure if this is how you want it though)

Thanks guys, I allready took the hard way:

x_domyslne='insert into prodzina (ID_prac,stan_cywilny,ilosc_dzieci,ubezpieczenie) values ((select id_prac from ppracownicy where nazwisko='+a+nazw+a+' and imie='+a+imie+a+' and id_dzial='+a+iddzial+a+' and pensja='+a+pensja+a+' and stanowisko='+a+stan+a+'),'+a+'kawaler'+a+',0,false))'

but I guess I'll rewrite it your way :)
Topic closed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.