I have such a code,
cursor_local.execute("""SELECT "Name", "Level_Capacity", "Source_NE", "Sink_NE" FROM "table1" WHERE "Name" LIKE '%WDM%' """)
rows = cursor_local.fetchall()
psycopg2.extras.execute_batch(cursor_local, 'INSERT INTO table2("Name", "Level_Capacity", "Source_NE", "Sink_NE") VALUES (%s, %s, %s, %s)', (*rows,) )
connection_local.commit()
and I would like to insert only new records into the table2 from table1 (postgres db), only those that are not in table2.
In table2 "Id" is automatically generated and is unique (others are not unique) , when inserting I would like to check if it exists "Name", "Source_NE", "Sink_NE".
is "name" unique? maybe you can do an ON CONFLICT Name DO NOTHING
INSERT INTO table2
VALUES ,,,
ON CONFLICT 'Name' DO NOTHING;
else you could do a NOT IN (SELECT "Name" etc FROM table2) etc
INSERT INTO table2
VALUES (
SELECT * FROM table1 WHERE ('Name' etc)
NOT IN (SELECT 'Name', etc FROM table2)
)
it works:
INSERT INTO "table2" ("Name", "Level_Capacity", "Source_NE", "Sink_NE")
SELECT "Name", "Level_Capacity", "Source_NE", "Sink_NE"
FROM "table1" WHERE ("Name", "Source_NE", "Source_Port", "Sink_NE", "Sink_Port") NOT IN (SELECT "Name", "Source_NE", "Source_Port", "Sink_NE", "Sink_Port" FROM "table2");
thank you
Related
i have an sql insert query that take values from user input and also insert the ID from another table as foreign key. for this is write the below query but it seems not working.
Status_type table
CREATE TABLE status_type (
ID int(5) NOT NULL,
status varchar(50) NOT NULL
);
info table
CREATE TABLE info (
ID int(11) NOT NULL,
name varchar(50), NULL
nickname varchar(50), NULL
mother_name varchar(50), NULL
birthdate date, NULL
status_type int <==this must be the foreign key for the status_type table
create_date date
);
for the user he has a dropdownlist that retrieve the value from the status_type table in order to select the value that he want to insert into the new record in the info table
where as the info table take int Type because i want to store the ID of the status_type and not the value
code:
query = '''
INSERT INTO info (ID,name,nickname,mother_name,birthdate,t1.status_type,created_date)
VALUES(?,?,?,?,?,?,?)
select t2.ID
from info as t1
INNER JOIN status_type as t2
ON t2.ID = t1.status_type
'''
args = (ID,name,nickname,mother_name,db,status_type,current_date)
cursor = con.cursor()
cursor.execute(query,args)
con.commit()
st.success('Record added Successfully')
the status_type field take an INT type (the ID of the value from another table ).
So when the user insert it insert the value.
What i need is to convert this value into its corresponding ID and store the ID
based on the answer of #Mostafa NZ I modified my query and it becomes like below :
query = '''
INSERT INTO info (ID,name,nickname,mother_name,birthdate,status_type,created_date)
VALUES(?,?,?,?,?,(select status_type.ID
from status_type
where status = ?),?)
'''
args = (ID,name,nickname,mother_name,db,status_type,current_date)
cursor = con.cursor()
cursor.execute(query,args)
con.commit()
st.success('Record added Successfully')
When creating a record, you can do one of these ways.
Receive as input from the user
Specify a default value for the field
INSERT INTO (...) VALUES (? ,? ,1 ,? ,?)
Use a select in the INSERT
INSERT INTO (...) VALUES (? ,? ,(SELECT TOP 1 ID FROM status_type ODER BY ID) ,? ,?)
When INSERT data, you can only enter the names of the destination table fields. t1.status_type is wrong in the following line
INSERT INTO info (ID,name,nickname,mother_name,birthdate,t1.status_type,created_date)
i have this part of a python script to create tables into a mysql db
#Product
TABLES['product'] = (
"CREATE TABLE product"
"(prod_id smallint,"
"name varchar(255),"
"price int,"
"constraint pk_prod_id primary key (prod_id)"
");")
#Sales
TABLES['sales'] = (
"CREATE TABLE sales"
"(sales_id smallint,"
"name varchar(255),"
"quantity int,"
"buy_id smallint,"
"date DATE,"
"constraint pk_sales_id primary key (sales_id,name)"
");")
#Purchase
TABLES['purchase'] = (
"CREATE TABLE purchase"
"(purchase_id smallint,"
"name varchar(255),"
"quantity int,"
"sup_id smallint,"
"date DATE,"
"constraint pk_purchase_id primary key (purchase_id,name)"
");")
# Adding foreign key
query = 'ALTER TABLE sales ADD foreign key(buy_id) references buyers(buy_id)'
cursor.execute(query)
query = 'ALTER TABLE purchase ADD foreign key(sup_id) references suppliers(sup_id)'
cursor.execute(query)
Until here it works ok, but here is the main problem.
query = 'ALTER TABLE sales ADD foreign key(name) references product(name)'
cursor.execute(query)
query = 'ALTER TABLE purchase ADD foreign key(name) references product (name)'
cursor.execute(query)
the error code is 1215, so can't add foreign keys
It works if i do this
query = ('ALTER TABLE sales ADD foreign key(prod_id) references product(prod_id)')
cursor.execute(query)
query = ('ALTER TABLE purchase ADD foreign key(prod_id) references product(prod_id)')
cursor.execute(query)
but i prefer to work with name and not with prod_id because it's a mess.
how can i solve this issues ? i tried with int type, but i don't like this solution, moreover i have to rewrite a lot of query.
Make the table product like this with KEY (name)
#Product
TABLES['product'] = (
"CREATE TABLE product"
"(prod_id smallint,"
"name varchar(255),"
"price int,"
"constraint pk_prod_id primary key (prod_id),"
"KEY (name)"
");")
And you can without probems add a foreign key to product name
thank you, it works.
But the previous query now doesn't work anymore
for istance i could do this;
cursor = cnx.cursor()
date = str(input('insert a date in yyyy-mm-dd :'))
query = "SELECT SUM(DISTINCT p.price*s.quantity),COUNT(p.price*s.quantity) FROM product p JOIN sales s ON p.name = s.name WHERE s.date > "
cursor.execute(query+"'"+date+"'")
for i,j in cursor:
print("Number of sales {}\nMoney earned {}".format(j,i))
and the out put was this
insert a date in yyyy-mm-dd :2019-06-03
Number of sales 5
Money earned 12750
now this doesn't work anymore.
I am trying to connecting and inserting a data from json to sqlite. But it sucks me
I have tried to move and avoiding white spaces and copmleting with the databases
conn=sqlite3.connect('rosterdb.sqlite')
cur=conn.cursor()
cur.execute(
'''
DROP TABLE IF EXISTS USER;
DROP TABLE IF EXISTS Course;
DROP TABLE IF EXISTS Member;
CREATE TABLE User (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
name TEXT UNIQUE
);
CREATE TABLE Course (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
title TEXT UNIQUE
);
CREATE TABLE Member (
user_id INTEGER,
course_id INTEGER,
role INTEGER,
PRIMARY KEY (user_id, course_id)
)
'''
)
fname=input('Enter file name')
if len(fname)<1:
fname=roster_data.json
str_data=open(fname).read()
json_data=json.loads(str_data)
for entry in json_data:
name=entry[0]
title=entry[1]
role=entry[2]
print(name,title,role)
cur.execute('''INSERT OR IGNORE INTO User (name)
VALUES ( ? )''', ( name, ) )
cur.execute('SELECT id FROM User WHERE name = ? ', (name, ))
user_id = cur.fetchone()[0]
cur.execute('''INSERT OR IGNORE INTO Course (title)
VALUES ( ? )''', ( title, ) )
cur.execute('SELECT id FROM Course WHERE title = ? ', (title, ))
course_id = cur.fetchone()[0]
cur.execute('''INSERT OR IGNORE INTO Member(user_id,course_id,role)
VALUES(?,?,?)''',(user_id,course_id,role))
conn.commit()
File "roster.py", line 51
cur.execute('''INSERT OR IGNORE INTO Member(user_id,course_id,role)
^
IndentationError: unindent does not match any outer indentation level
You are probably mixing tabs and spaces
copy this to your editor:
cur.execute('''INSERT OR IGNORE INTO Member(user_id,course_id,role)
VALUES(?,?,?)''',(user_id,course_id,role))
And manually press space bar 4 times before cur.execute
And manually press space bar 8 times before VALUES
cur.execute('''INSERT OR IGNORE INTO Member(user_id,course_id,role)
VALUES(?,?,?)''',(user_id,course_id,role))
Please don't use tabs in combination with spaces
I use python 2.7 + Psycopg2 2.6 to insert data into a Postgresql 9.4 database, which is working fine on a very basic level. Have created some dynamic INSERT-queries, which take different sets of columns and values from a dictionary (input_cols):
sql_template = "insert into tbl ({}) values %s"
sql = sql_template.format(', '.join(input_cols.keys()))
params = (tuple(input_cols.values()),)
cur.execute(sql, params)
Correct SQL generated:
insert into tbl (col1, col2, ...) values ('val1', 'val2', ...)
Would now like to use dynamic SQL generation also for some INSERT if NOT EXIST queries, but as 'cur.execute(sql, params)' above outputs a value list enclosed by '()' I cannot get it to work:
sql_template = "insert into tbl ({}) select %s where not exists (select id
from tbl where id = %s)"
sql = sql_template.format(', '.join(input_cols.keys()))
params = (tuple(input_cols.values()), input_cols['col1'])
Incorrect SQL generated:
insert into tbl (col1, col2) select ('val1', 'val2')
where not exists (select col1 from tbl where id = 'val1')
How can I output ('val1', 'val2') without () so that I can use it in a SELECT xxx, xxx WHERE NOT EXISTS query?
Use from (values...
input_cols = {'col1':'val1','col2':'val2'}
sql_template = """
insert into tbl ({})
select *
from (values %s) s
where not exists (
select id
from tbl
where id = %s
)
"""
sql = sql_template.format(', '.join(input_cols.keys()))
params = (tuple(input_cols.values()), input_cols['col1'])
print cursor.mogrify(sql, params)
Output:
insert into tbl (col2, col1)
select *
from (values ('val2', 'val1')) s
where not exists (
select id
from tbl
where id = 'val1'
)
I`m just started to learn SQLite. I use python.
The question is how to create rows in tables, so that they are uniqe by name and how to use (extract) id1 and id2 to insert them into a separate table.
import sqlite3
conn = sqlite3.connect('my.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS table1(
id1 integer primary key autoincrement, name)''')
c.execute('''CREATE TABLE IF NOT EXISTS table2(
id2 integer primary key autoincrement, name)''')
c.execute('CREATE TABLE IF NOT EXISTS t1_t2(id1, id2)') # many-to-many
conn.commit()
conn.close()
conn = sqlite3.connect('my.db')
c = conn.cursor()
c.execute('INSERT INTO table1 VALUES (null, "Sue Monk Kidd")')
c.execute('INSERT INTO table2 VALUES (null, "The Invention of Wings")')
#c.execute('INSERT INTO t1_t2 VALUES (id1, id2)')
c.execute('INSERT INTO table1 VALUES (null, "Colleen Hoover")')
c.execute('INSERT INTO table2 VALUES (null, "Maybe Someday")')
#c.execute('INSERT INTO t1_t2 VALUES (id1, id2)')
Thanks.
I think you have some problems with the table creation. I doubt that it worked, because the name columns don't have a type. They should probably be varchar of some length. The JOIN table definition isn't right, either.
CREATE TABLE IF NOT EXISTS table1 (
id1 integer primary key autoincrement,
name varchar(80)
);
CREATE TABLE IF NOT EXISTS table2 (
id2 integer primary key autoincrement,
name varchar(80)
);
CREATE TABLE IF NOT EXISTS t1_t2 (
id1 integer,
id2 integer,
primary key(id1, id2),
foreign key(id1) references table1(id1),
foreign key(id2) references table2(id2)
);
I would not create the tables in code. Script them, execute in the SQLite admin, and have the tables ready to go when your Python app runs.
I would think much harder about your table names if these are more than examples.
I found the problem of unique names on unique column problem.
Actually, I should change INSERT to INSERT OR IGNORE
import sqlite3
conn = sqlite3.connect('my.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS table1(
id1 integer primary key autoincrement, name TEXT unique)''')
c.execute('''CREATE TABLE IF NOT EXISTS table2(
id2 integer primary key autoincrement, name TEXT unique)''')
c.execute('CREATE TABLE IF NOT EXISTS t1_t2(id1, id2)') # many-to-many
conn.commit()
conn.close()
conn = sqlite3.connect('my.db')
c = conn.cursor()
c.execute('INSERT OR IGNORE INTO table1 VALUES (null, "Sue Monk Kidd")')
c.execute('INSERT OR IGNORE INTO table2 VALUES (null, "The Invention of Wings")')