I am trying to execute some Postgres queries with psycopg2.
import psycopg2
conn = psycopg2.connect(
host="test.postgres.database.azure.com",
database="postgres",
user="test",
password="test")
cur = conn.cursor()
sql = "select site_panel from test.ws_sites"
cur.execute(sql)
rows = cur.fetchall()
The query above works well and returns the data but the following query does not delete the table as intended.
cur.execute ("DROP TABLE IF EXISTS test.ws_sites")
Anything wrong I'm doing here?
A query that modifies table data or schema structure needs to be committed:
cur.execute ("DROP TABLE IF EXISTS test.ws_sites")
conn.commit()
Alternatively, place
conn.autocommit = True
after creating the connection.
Related
I have parameterized queries with f strings such that the queries will select some data from a series of tables and joins, and I want to insert the resulting set of data into another pre-created table (tables been designed to house these results).
Python executes the code but the query results never show up in my table.
Assuming target_table is already created in singlestore database:
qry_load = 'insert into target_table select * from some_tables'
conn = engine.connect()
trans = conn.begin()
try:
conn.execute(qry_load)
trans.commit()
except:
trans.rollback()
raise
The code executes and acts as if all is ok, but the data never shows up in the target table.
How do I see what singlestore is passing back to better debug what is happening within the database?
Just replace begin() with cursor() function:
conn = engine.connect()
trans = conn.cursor()
If not resolved
1- Verify structure of source and destination tables if they are same or not.
2- remove try ,except and rollback() block so you can know the actual error.
Ex.
qry_load = 'insert into target_table select * from some_tables'
conn = engine.connect()
trans = conn.cursor()
conn.execute(qry_load)
trans.commit()
i've been trying to get some data from my db by using below code, but the code is not working. is there any mistake that i made in the code, if so how can i fix it.
NOTE: i took the below code from just a script not a django or flesk web app.
def db():
conn = psycopg2.connect(
"dbname=mydb user=postgres password=****** host=*.*.*.*")
cur = conn.cursor()
cur.execute("""SELECT * FROM MddPublisher""")
query_results = cur.fetchall()
print(query_results)
db()
ERROR: psycopg2.errors.UndefinedTable: relation "mddpublisher" does not exist LINE 1: SELECT * FROM MddPublisher
additionally,i want to show below code to prove that connection is ok. the problem is that i can't receive data from my db whenever i try to execute select command through python.
def print_tables():
conn = psycopg2.connect(
"dbname=mydb user=postgres password=***** host=*.*.*.*.*")
cur = conn.cursor()
cur.execute("""SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public'""")
for table in cur.fetchall():
print(table)
print_tables()
OUTPUT:
('MddPublisher',)
This is probably an issue with case sensitivity. Postgresql names are usually normalized to lower case. However, when used inside double quotes, they keep their case. So, to access a table named MddPublisher you must write it like "MddPublisher".
All the gory details are in Section 4.1.1, Identifiers and Key Words in the Postgresql 14 docs.
I want to insert values into a SQL Server table, from Python.
Here is my table in SQL Server:
CREATE TABLE DB.type.[Ref]
(
[Type_Ref] varchar(65),
[ID_match] varchar(65),
[Data] varchar(65)
PRIMARY KEY ([Type_Ref], [ID_match])
)
To insert values from python I wrote this code:
conn = pyodbc.connect('Driver={ODBC Driver 17 for SQL Server};'
'Server=????;'
'Database=DB;'
'Trusted_Connection=yes;')
cursor = conn.cursor()
sql = "INSERT INTO DB.Ref (Type_Ref, ID_match, Data) VALUES (?, ?, ?)"
cursor.execute(sql, (DATA[Type_Ref], DATA[ID_match], DATA[Data]))
conn.commit()
cursor.close()
conn.close()
However, when I run Python, I don't see any rows of data in SQL Server...
I noticed my IDE is giving this, after I run the python code above:
Process finished with exit code -1073741819 (0xC0000005)
Solution: I solved this problem by deleting the table in sql-server and create a new one.
I think you are not building your conn object correctly. When I try to match your pattern, I receive a TypeError. Try this instead:
server='server'
database='database'
user='username'
password='secret'
conn = pyodbc.connect(
server=server,
database=database,
user=username,
password=password,
port=1433,
driver='ODBC Driver 17 for SQL Server'
)
The rest of your code looks like it should work fine.
try the following:
Columns = ['Type_Ref', 'ID_match', 'Data']
sql = "INSERT INTO dbo.Ref([Type_Ref], [ID_match], [Data]) VALUES (?,?,?)"
cursor.executemany(sql, DATA[Columns].values.tolist())
conn.commit()
Thaks for the help, I solved this problem by deleting the table in sql-server and create a new one.
I would to remove all customers who's names begin with SCH from my database. When I execute the code below it runs without error, but does not remove the data from the database.
cur = db.cursor()
sql = "DELETE FROM customers where IMAGE_ID like 'SCH%'"
cur.execute(sql)
after delete you need commit
conn = cx_Oracle.connect(...)
cur = db.cursor()
sql = "DELETE FROM customers where IMAGE_ID like 'SCH%'"
cur.execute(sql)
conn.commit()
cur.close()
conn.close()
I'm using the Python package MySQLdb to fetch data from a MySQL database. However, I notice that I can't fetch the entirety of the data.
import MySQLdb
db = MySQLdb.connect(host=host, user=user, passwd=password)
cur = db.cursor()
query = "SELECT count(*) FROM table"
cur.execute(query)
This returns a number less than what I get if I execute the exact same query in MySQL Workbench. I've noticed that the data it doesn't return is the data that was inserted into the database most recently. Where am I going wrong?
You are not committing the inserted rows on the other connection.