python sqlite create id INTEGER PRIMARY KEY AUTOINCREMENT - python

Creating a simple database and have the rows with an id so I can select row values later:
conn = sqlite3.connect("APIlan.db")
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS ENERGYLOG (id INTEGER PRIMARY KEY AUTOINCREMENT, totalenergy REAL)''')
c.execute("INSERT INTO ENERGYLOG VALUES (?);", (total_energy,))
conn.commit()
conn.close()
Error sqlite3.OperationalError: table ENERGYLOG has 2 columns but 1 values were supplied
Second try:
conn = sqlite3.connect("APIlan.db")
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS ENERGYLOG (id INTEGER PRIMARY KEY AUTOINCREMENT, totalenergy REAL)''')
c.execute("INSERT INTO ENERGYLOG VALUES (?,?);", (NULL,total_energy,))
conn.commit()
conn.close()
Error NameError: name 'NULL' is not defined
Without supplying the value for id, how do I get it into the table? Thanks.

I have two solutions.
1.Your first try, if you only want to insert the columns you choose, you can follow this syntax:
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)] VALUES (value1, value2, value3,...valueN);
so, you can write this:
c.execute("INSERT INTO ENERGYLOG (totalenergy) VALUES (?);", (total_energy,))
2.Your second try, if you want to insert all columns, you can replace 'NULL' to 'None':
c.execute("INSERT INTO ENERGYLOG VALUES (?, ?);", (None, total_energy))
Because python don't know 'NULL'.
In SQL we use 'NULL' and in python we use 'None'.
hope it can help you!

You should explicitly list what columns you want to insert into:
c.execute("INSERT INTO ENERGYLOG (totalenergy) VALUES (?);", (total_energy,))
As far as parameterizing NULL, you should specify None as the parameter value:
c.execute("INSERT INTO ENERGYLOG VALUES (?, ?);", (None, total_energy))
Or, use NULL and a single parameter:
c.execute("INSERT INTO ENERGYLOG VALUES (NULL, ?);", (total_energy,))

Related

How to insert nulls into a SQL Server table

I have the following dataframe:
data = [['Alex', 182.2],['Bob', 183.2],['Clarke', 188.4], ['Kelly', NA]]
df = pd.DataFrame(data, columns = ['Name', 'Height'])
I have the following SQL Server table:
create table dbo.heights as (
name varchar(10),
height float
)
This is my code to upload the data to my table:
for index,row in df.iterrows():
cursor.execute('INSERT INTO dbo.heights(name, height) values (?, ?)', row.name, row.height)
cnxn.commit()
cursor.close()
cnxn.close()
I want to upload the dataframe into my SQL Server table, but it fails on the null value. I tried replacing the NA with an np.nan value and it still failed. I also tried changing the height column to an "object" and replacing the NA with None and that also failed.
Please use the following instead:
for index, row in df.iterrows():
query = "INSERT INTO dbo.heights(name, height) values (?, ?)"
data = [row.name, row.height]
cursor.execute(query, data)
cursor.commit()
Or use the following:
query = "INSERT INTO dbo.heights(name, height) values (?, ?)"
data = [row.name, row.height for index, row in df.iterrows()]
cursor.executemany(query, data)
cursor.commit()
You'll see your None values as None in Python and as NULL in your database.
I tried replacing the NA with an np.nan
Because in such case you have to first define dataframe schema and make it nullable float.
"By default, SeriesSchema/Column objects assume that values are not nullable. In order to accept null values, you need to explicitly specify nullable=True, or else you’ll get an error."
Further Reading
Try like this
for index,row in df.iterrows():
cursor.execute("INSERT INTO table (`name`, `height`) VALUES (%s, %s)", (row.name, row.height))
cnxn.commit()
cursor.close()
cnxn.close()

python sqlite3 parameterization - insert throws no such column error

Insert in columns with parameterized query throws no such column error
First (working) example:
# unit test input
name = "issue_number_1"
text = "issue_text"
rating_sum = 0
if name:
# check if issue is already in db
with self.conn: # this should release the connection when finished
test = cursor.execute("SELECT name, text FROM issue WHERE name = ?", (name,))
data = test.fetchall()
print(data)
this is working and prints:
[('issue_number_1', 'issue_text')]
Second (non working) example:
# unit test input
name = "issue_number_2"
text = "issue_text"
rating_sum = 0
if name:
with self.conn:
sql_string = "INSERT INTO issue (name, text, rating_sum) VALUES (name = ?, text = ?, rating_sum = ?)"
cursor.execute(sql_string, (name, text, rating_sum,))
throws this error:
cursor.execute(sql_string, (name, text, rating_sum,))
sqlite3.OperationalError: no such column: name
the column name exists, the first example proofed that
the name: "issue_number_2" does not exist in the DB
the second example fails exactly same with only name to insert (only one parameter)
i had no problems inserting with string concatenation so the problem should be in my second example code somewhere
You need to add single quote.for example:
"INSERT INTO table (field) VALUES ('$1')"
add just values in second () and add single quote around string values.
After a lot of experiments i was a little bit confused....
This is the right syntax:
sql_string = "INSERT INTO issue (name, text, rating_sum) VALUES (?, ?, ?)"
cursor.execute(sql_string, (name, text, rating_sum,))
The statement:
INSERT INTO .... VALUES ....
is an SQL statement and the correct syntax is:
INSERT INTO tablename (col1, col2, ...) VALUES (expr1, expr2, ...)
where col1, col2, ... are columns of the table tablename and expr1, expr2, ... are expressions or literals that are evaluated and assigned to each of the columns col1, col2, ... respectively.
So the syntax that you use is not valid SQL syntax.
The assignment of the values is not performed inside VALUES(...).
The correct syntax to use in Python would be:
INSERT INTO issue (name, text, rating_sum) VALUES (?, ?, ?)

insert row if not exist in database

Hello how can i do to insert uniq rows without duplicate.
cursor.execute("CREATE TABLE IF NOT EXISTS tab1 (id varchar(36) primary key, cap1 VARCHAR(4), cap2 varchar(55), cap3 int(6), Version VARCHAR(4));")
id = uuid.uuid1()
id = str(id)
cursor.execute("INSERT IGNORE INTO tab1 (id, cap1, cap2, cap3, Version) VALUES (%s, %s, %s, %s, %s )", (vals))
I should not insert the third row while is the same as first row.
Hope im clear .
Thank you in advance,
The problem is that uuid() will always give a unique identifier and since id is a primary key, the row is getting inserted with duplicate values except for id column which is different always.
I think this link might answer your question or else, create a unique index on columns that you want to be unique.
Let me know if it helps!!

SQLite with Python "Table has X columns but Y were supplied"

I have a python script that executes some simple SQL.
c.execute("CREATE TABLE IF NOT EXISTS simpletable (id integer PRIMARY KEY, post_body text, post_id text, comment_id text, url text);")
command = "INSERT OR IGNORE INTO simpletable VALUES ('%s', '%s', '%s', '%s')" % (comments[-1].post_body, comments[-1].post_id, comments[-1].comment_id,
comments[-1].url)
c.execute(command)
c.commit()
But when I execute it, I get an error
sqlite3.OperationalError: table simpletable has 5 columns but 4 values were supplied
Why is it not automatically filling in the id key?
In Python 3.6 I did as shown below and data was inserted successfully.
I used None for autoincrementing ID since Null was not found.
conn.execute("INSERT INTO CAMPAIGNS VALUES (?, ?, ?, ?)", (None, campaign_name, campaign_username, campaign_password))
The ID structure is as follows.
ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL
If you don't specify the target columns VALUES is expected to provide values for all columns and that you didn't do.
INSERT
OR IGNORE INTO simpletable
(text,
post_id,
comment_id,
text)
VALUES ('%s',
'%s',
'%s',
'%s');
Specifying the target columns is advisable in any case. The query won't break, if, for any reason, the order of the columns in the tables changes.
try to specify the columns names to ensure that the destination of values doesn't depends on order.
ex:
INTO simpletable
(text,
post_id,
comment_id,
text)
And if you wants the id column to be automatically incremented make sure to add Identity property on, or similar auto increment of your dbms.
ex:
CREATE TABLE IF NOT EXISTS simpletable (id integer PRIMARY KEY Identity(1,1),
and remember your script is not prepared to alter the table structure, only creation.
If you wrote code correctly delete your SQL file(name.db) and run your code again some time it solve the problem.
Imagine this is your code:
cursor.execute('''CREATE TABLE IF NOT EXISTS food(name TEXT , price TEXT)''')
cursor.execute('INSERT INTO food VALUES ("burger" , "20")')
connection.commit()
and you see an error like this:
table has 1 column but 2 values were supplied
it happened because for example you create a file with one column and then you modify your file to two column but you don't change the file name so compiler do not over write it because it exist.

Insert data in one table within multiple app at the same time with MySQL

I have written eight source code to insert some value to a database called "TA" in one table called "8box". I am planning to run this eight source code at the same time,and then this eight source code keeps storing data in database in almost one hour. I wrote this code for collecting data, and each of this eight source code have the same syntax to insert values to "8box"
I want to run this eight source code at the same time to be able to collect data.
I tried this and the result is that the database is just filled with values from first source code, none values from another source stored in database
What should i do ?
waktu=time.strftime('%Y-%m-%d %H:%M:%S')
con = mdb.connect('localhost', 'root', 'qwer1234', 'TA');
with con:
cur = con.cursor()
#cur.execute("DROP TABLE IF EXISTS 8Box")
#cur.execute("CREATE TABLE 8Box (Name VARCHAR(25),Lot_Bid INT,Bid INT,Offer INT,Lot_Offer INT,Time DATETIME)")
cur.execute("INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)",(name_new,lot_bid_1_new,bid_1_new,off_1_new,lot_off_1_new,waktu))
cur.execute("INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)",(name_new,lot_bid_2_new,bid_2_new,off_2_new,lot_off_2_new,waktu))
cur.execute("INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)",(name_new,lot_bid_3_new,bid_3_new,off_3_new,lot_off_3_new,waktu))
cur.execute("INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)",(name_new,lot_bid_4_new,bid_4_new,off_4_new,lot_off_4_new,waktu))
cur.execute("INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)",(name_new,lot_bid_5_new,bid_5_new,off_5_new,lot_off_5_new,waktu))
cur.execute("INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)",(name_new,lot_bid_6_new,bid_6_new,off_6_new,lot_off_6_new,waktu))
cur.execute("INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)",(name_new,lot_bid_7_new,bid_7_new,off_7_new,lot_off_7_new,waktu))
cur.execute("INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)",(name_new,lot_bid_8_new,bid_8_new,off_8_new,lot_off_8_new,waktu))
cur.execute("INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)",(name_new,lot_bid_9_new,bid_9_new,off_9_new,lot_off_9_new,waktu))
cur.execute("INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)",(name_new,lot_bid_10_new,bid_10_new,off_10_new,lot_off_10_new,waktu))
You should use the MySQLdb interface and not the _mysql interface. I would code it as:
cn = MySQLdb.connect(...)
c = cn.cursor()
try:
c.executemany("""
INSERT INTO 8Box VALUES (%s,%s,%s,%s,%s,%s)
""", [
(name_new,lot_bid_1_new,bid_1_new,off_1_new,lot_off_1_new,waktu),
(name_new,lot_bid_2_new,bid_2_new,off_2_new,lot_off_2_new,waktu),
# .. etc.
(name_new,lot_bid_10_new,bid_10_new,off_10_new,lot_off_10_new,waktu)
])
cn.commit()
except:
cn.rollback()
raise
Update: here's a screendump of the two processes running simultaneously:

Categories