I have a question regarding performing insert into a table in BigQuery using the DBAPI .
The simple insert of 1 value I was able to preform , my question is how to preform insert of list of values.
I couldn't find an example on how to do it, I want to preform something like :
query ="insert into test.test values (%s)"
self._cursor.execute(query,(('4'),('3')))
But I am getting an error
Thanks,
Nir
You can try the command executemany. This method is used to insert multiple rows in a single operation.
Your code should look something like:
query ="insert into test.test values (%s)"
self._cursor.executemany(query,[('4'),('3')])
Also what is the error that you are getting?
I'm trying to create a table in python using azure sql server, when i run this line of code below i get the following error:
'There is already an object named '***' in the database'.
def del_final_trades(new_table_name, cl_table_name, cp_table_name):
cursor.execute('''SELECT * INTO {newt} FROM {cl_t} INNER JOIN {cp_t} ON
{cl_t}.[CL_Trade ID] = {cp_t}.[CP_Trade ID]'''.format(newt=new_table_name, cl_t=cl_table_name,
cp_t=cp_table_name))
The error is correct, the table does exist but how do I drop the table if its NOT NULL and create a new one?
I understand the logic, I'm just struggling to put it into a working code, any help is appreciated!
You need to test if the table exists within a drop command:
DROP TABLE IF EXISTS [Table name]
I've created a login function with flask, it consists of a code that opens a connection to the database then selecting a username based on the inputed username. To be more precise, here's the query:
cursor.execute("""
SELECT username FROM user_tbl
WHERE username = %s""",
(self.username))
After that, I check the length of the fetched data, if length is 0 then I open up another connection then perform an insert query, like so:
cursor.execute("""
INSERT INTO
user_tbl(username,password,email,user_type)
VALUES(%s,%s,%s,%s)""",
(self.username,self.password,self.email,self.user_type))
I've been doing this process since coding with PHP and would like to confirm if there is any way to combine these two queries. I've been researching like crazy and can't seem to find the answer... or atleast answers that work.
MySQL direct INSERT INTO with WHERE clause based on the accepted answer there, INSERT INTO...SELECT is the way to go, however after looking into it its mostly about transferring data from another table to another, I am targeting one table (my apologies if I'm missing something ).
I can't find the link, however I found another answer that mentioned that the only time you'll see a WHERE clause in an INSERT query(aside from the answer I posted above) is when you're checking if nothing 'EXISTS' (which makes sense and based on that answer I made the conclusion that having a where clause in an insert query is ok).
After checking up subquerying on a WHERE clause and following examples in this link: https://www.essentialsql.com/get-ready-to-learn-sql-server-21-using-subqueries-in-the-where-clause/ I've created my own query:
INSERT INTO user_tbl(username,password,email,user_type)
VALUES("test.test","test","test","test")
WHERE username IN
(SELECT username FROM user_tbl WHERE username="test.test");
Reason why I chose IN is because, as mentioned in the link, once a subquery returns NULL, IN returns false for the WHERE clause (at least that's how I interpreted it).
Unfortunately, each time I run this code on my terminal I get this syntax error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE username IN (SELECT username FROM user_tbl WHERE username="test.test")'
Hence the title, Can you guys please explain how exactly is my code syntactically wrong? Also, can you perchance pinpoint me to the right direction as I am very lost on this.
Thanks in advance,
Inno
I understand that you want to INSERT a new record in user_tbl only if it does not yet exist.
MysQL has a special syntax for that, called INSERT ... ON DUPLICATE KEY UPDATE.
For this to work, you need column username to be the primary key in your table (or to have a UNIQUE constraint).
Then you can simply do:
cursor.execute(
"""
INSERT INTO user_tbl(username,password,email,user_type)
VALUES(%s,%s,%s,%s)
ON DUPLICATE KEY UPDATE username = VALUES(username)
""",
(self.username,self.password,self.email,self.user_type)
)
If no record aleady exists for the given username, a new record is created. Else, the UPDATE clause is invoked (here, that would simply reassign the same value to the username , which is basically a no-op).
you cant use the same table, but you can "hide" the SELECT then MySQL did not see this like:
INSERT INTO user_tbl(username,password,email,user_type)
VALUES("test.test","test","test","test")
WHERE username IN
(SELECT * FROM (
SELECT username FROM user_tbl WHERE username="test.test"
) as myuser
);
i need to recreate indexes on a table as i have to insert a lot of data into the table.
i am trying to get the defination of an index in postgres using
SELECT pg_get_indexdef('start_date_sr_index_its'::regclass);
it works, but when i try to run this same command from psycopg2 it says relation does not exist
psycopg2.ProgrammingError: relation "start_date_sr_index_its" does not exist
LINE 1: SELECT pg_get_indexdef('start_date_sr_index_its'::regclass);
^
i have tried to replace ' with " but it says the same
An easier way to get index definition in postgres is to get it directly through the pg_index table instead of using utility function pg_get_indexdef().
You can simply query
SELECT indexdef FROM pg_indexes WHERE indexname = ''
you can also get schemaname, tablename and tablespace from this table.
I'm trying to run a simple insert query to a database. I have it configured correctly, and it should work, but it doesn't. For some reason I get the following error on this query:
Query:
INSERT INTO searches (query) VALUES ('test')
Error:
(1062, "Duplicate entry 'test' for key 'query'")
The query runs without problems in the MySQL console so it must be a problem with Python? Here's my Python code:
def increase_search_count(search_query):
from django.db import connection, transaction
search_query = search_query.strip()
cursor = connection.cursor()
rows = cursor.execute("INSERT INTO searches (query) VALUES ('test')")
I know there are much better ways to handle databases, but I'm new to Python, and I have a deadline. I'd just like to get this to work, I have another SELECT query in another function and that one runs without any problems!
Any ideas what might be wrong?
The way that query is constructed means you will always be inserting 'test' into the database, and seeing the query is likely the primary key in your table, it will be creating duplicate rows.
The query should be something like "INSERT INTO searches (query) VALUES ('" variable "')" so you don't insert the same value over and over.