I want to connect to a postgres database in a specific squema using psycopg2. I read this thread but I get a syntax error.
schema = conf['schema']
conn = psycopg2.connect(
dbname=conf['database'],
user=conf['user'],
host=conf['host'],
password=conf['passw'],
port=conf['port'],
options=f'-c search_path={schema}',
)
I checked the config.json where I get the data.
Thanks
Related
I am attempting to create a flask web application that uses mysql to store users and some other data. When I use the following code
import MySQLdb
def connection():
conn = MySQLdb.connect(host='localhost', user='root', passwd='password', database='data')
c = conn.cursor()
return c, conn
I get a 500 internal service error. This code will connect on the command line, and my flask site works fine until I add the line
from dbconnect import connection
I have tried using different ways to connect to the database, (python connector, pymysql). They all give the same error. I have also tried updating permissions from inside mysql to the user. I have been following tutorials from https://pythonprogramming.net/flask-connect-mysql-using-mysqldb-tutorial/ but they did not get these errors in the tutorial and I have followed most of the tutorial exactly.
I have created a python file app.py and included the code to connect to a db I created in postgresql as follows: -
import psycopg2
conn = psycopg2.connect(
user='postgres',
password='1234',
host='localhost',
port='5432',
database='bubbleformation'
)
cursor = conn.sursor()
cursor.execute('SELECT * FROM bubbleformation')
for row in cursor: print(row)
conn.close()
This was as instructed in this medium article
However, when I try to execute this python file in the terminal, I get the below error: -
Traceback (most recent call last): File "app.py", line 8, in
port='5432' File "/usr/lib/python2.7/dist-packages/psycopg2/init.py", line 130, in
connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync) psycopg2.OperationalError: FATAL: database "bubbleformation" does not exist
I have created a table named "bubbleformation" and it can be viewed in the psql mode through the terminal.
Could anyone please help me understand what should be done? I tried changing the password, and user privileges, but none of them worked for my error.
You should create both database and table with the same name "bubbleformation". You've probably created that table in postgres database.
Enter psql as postgres user and call CREATE DATABASE bubbleformation;, then connect to it with \connect bubbleformation and then create your table (something like CREATE TABLE bubbleformation (id int, name text);).
The error is about there not being a database named "bubbleformation", so when you connect to the database in the terminal, whichever database that is is the one you need to specify in the database parameter. When you connect to the database in the terminal, type:
SELECT current_database();
If it is indeed a database named "bubbleformation" then is must be a different cluster you are connecting to, and therefore a different port.
Disclosure: I am an EnterpriseDB (EDB) employee.
Its due to environment error. I was loading the credentials from the .env file. But i mistakenly gave a wrong path.
project_folder = os.path.expanduser('~/scraping') instead of
project_folder = os.path.expanduser('~/find_my_nearest_store')
load_dotenv(os.path.join(project_folder, '.env'))
Hence the Error.
I'm trying to query an RDS (Postgres) database through Python, more specifically a Jupyter Notebook. Overall, what I've been trying for now is:
import boto3
client = boto3.client('rds-data')
response = client.execute_sql(
awsSecretStoreArn='string',
database='string',
dbClusterOrInstanceArn='string',
schema='string',
sqlStatements='string'
)
The error I've been receiving is:
BadRequestException: An error occurred (BadRequestException) when calling the ExecuteSql operation: ERROR: invalid cluster id: arn:aws:rds:us-east-1:839600708595:db:zprime
In the end, it was much simpler than I thought, nothing fancy or specific. It was basically a solution I had used before when accessing one of my local DBs. Simply import a specific library for your database type (Postgres, MySQL, etc) and then connect to it in order to execute queries through python.
I don't know if it will be the best solution since making queries through python will probably be much slower than doing them directly, but it's what works for now.
import psycopg2
conn = psycopg2.connect(database = 'database_name',
user = 'user',
password = 'password',
host = 'host',
port = 'port')
cur = conn.cursor()
cur.execute('''
SELECT *
FROM table;
''')
cur.fetchall()
I have a mysql database that I am currently trying to connect python to. I have successfully created a table with python but when it comes to inserting data in the table my code does not work.
import pymysql
conn = pymysql.connect(
host = Admin.Host,
user = Admin.User,
password= Admin.Password,
db = "thesystem"
)
a = conn.cursor()
sql ="INSERT INTO `a`(`try`) VALUES ('testing')"
a.execute(sql)
I have tried this exact command in the Mysql command Line client and it worked.
INSERT INTO `a`(`try`) VALUES ('testing');
I have also managed to create a table with this same code. I can't figure out why the code runs successfully but does not work.
The resolution is that pymysql requires that you call conn.commit() after making a change. By default, pymysql is not autocommit.
Code example at https://github.com/PyMySQL/PyMySQL says:
. . .
# connection is not autocommit by default. So you must commit to save
# your changes.
connection.commit()
. . .
I have a database that I am running on my local machine which I can access through Microsoft SQL Server Manager Studio. I connect to this server "JIMS-LAPTOP\SQLEXPRESS" and then I can run queries through the manager. However I need to be able to connect to this database and work with it through python.
When I try to connect using sqlite3 like
conn = sqlite3.connect("JIMS-LAPTOP\SQLEXPRESS")
I get an unable to open database file error
I tried accessing the temporary file directly like this
conn = sqlite3.connect("C:\Users\Jim Notaro\AppData\Local\Temp\~vs13A7.sql")
c = conn.cursor()
c.execute("SELECT name FROM sqlite_master WHERE type = \"table\"")
print c.fetchall()
Which allows me to access a database but it is completely empty (No tables are displayed)
I also tried connecting like this
conn = sqlite3.connect("SQL SERVER (SQLEXPRESS)")
Which is what the name is in the sql server configuration manager but that also returns a blank database.
I'm not sure how I am suppose to be connecting to the database using python
You can't use sqlite3 to connect to SQL server, only to Sqlite databases.
You need to use a driver that can talk to MS SQL, like pyodbc.