I tried to establish a connection to my sqlite3 database which is located on my laptop's hard drive. I used the follwing example code from https://sfu-db.github.io/connector-x/databases/sqlite.html:
import connectorx as cx
db_path = '/home/user/path/test.db' # path to your SQLite database
conn = 'sqlite://' + db_path # connection token
query = 'SELECT * FROM `database.dataset.table`' # query string
cx.read_sql(conn, query) # read data from SQLite
Since I'm using Windows the path to the database contains a colon:
db_path = 'E:/my_db.db'
conn = 'sqlite://E:/my_db.db'
When I try to execute cx.read_sql(conn, 'select * from tablename') I get the following error message:
[2023-01-21T19:01:47Z ERROR r2d2] unable to open database file: E/my_db.db
What's interesing about the error message is that the path to the database which it displays is missing the colon. It is E/my_db.db instead of E:/my_db.db. It looks like a bug. So the question is: How to establish the connection to my database on Windows?
I got the hint to use db_path = urllib.parse.quote(os.path.abspath(database_name)) but it didn't really help me out. Now I get the following error:
RuntimeError: Conversion error from type Text at index: 11, trailing input
Related
I've read a lot of articles about my problem but no one solve it. So u can see my code here
DATABASE_URL = os.environ.get('url_of_my_db')
con = None
try:
con = psycopg2.connect(DATABASE_URL)
cur = con.cursor()
print('PostgreSQL database version:')
#cur.execute('SELECT version()')
#cur.execute('SELECT * FROM qwerty')
#cur.execute(sql.SQL('SELECT * FROM {}').format(sql.Identifier('qwerty')))
#cur.execute(sql.SQL("INSERT INTO {} (chat_id, username, created_on) VALUES (8985972942, vovakirdan, 2022-01-05)").format(sql.Identifier('users')))
cur.execute("""INSERT INTO users (chat_id, username, created_on)
VALUES (3131,
vovakirdan,
2022-01-05)""")
# display the PostgreSQL database server version
db_version = cur.fetchone()
print(db_version)
# close the communication with the HerokuPostgres
cur.close()
except Exception as error:
print('Cause: {}'.format(error))
finally:
# close the communication with the database server by calling the close()
if con is not None:
con.close()
print('Database connection closed.')
and in my DB table named "users" (named without quotes) are exist, but I still have this error:
error
...relation "users" does not exist
all the #commented code doesn't work and send the same error besides SELECT version(), it works perfectly that proves that connection works.
The problem was that PostgreDB wants me to use SELECT colum FROM schema.table instead of SELECT colum FROM table. And that's all. Thanks everyone
So I'm following the NCBI instructions available here: https://www.ncbi.nlm.nih.gov/books/NBK52640/
and I can't for the life of me understand what's wrong here.
Here's my PATH and BLASTDB:
And the error message:
And my blastdb directory:
And here's my Python code:
from Bio.Blast.Applications import NcbipsiblastCommandline
import subprocess
psi_cline = NcbipsiblastCommandline('psiblast', db = 'refseq_protein.00',\
query = "results.fasta", evalue = 10 , \
out = "out_psi.xml", outfmt = 7, \
out_pssm ="pssm-results_pssm")
print(psi_cline)
I had this very problem today and it turns out that my blast-database was corrupted in some manner. I recreated my database with makeblastdb and this error went away.
I have a .sqlite file which is protected by a password. When I use viewer like SQLiteStudio to connect it, I need to manually input the password in UI.
When I use c# to connect it, the codes as below work very well:
string db_file_path = "xxx.sqlite";
string conn_conf = $"Data Source={db_file_path};Version=3;password={db_passwd}";
SQLiteConnection conn = new SQLiteConnection(conn_conf);
But for python, the code is like this:
sqlite_file = '/xxx.db'
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
"OperationalError: unable to open database file" appear on the connect()
So, how to use the password??? How to open it???
Could someone help me with the below error while connecting to Teradata from my Python environment.
I'm using ODBC driver method and I've tried all the below existing methods to connect but no luck.
Note: if you are using windows, you can directly use these methods, however the problem comes when you are on MAC OS (not for all though)
USING TERADATA Module and SQL Alchemy.
import teradata
import pyodbc
server='111.00.00.00'
username = 'user'
password = 'pwd'
udaExec = teradata.UdaExec(appName="test", version="1.0",
logConsole=True)
ndw_con = udaExec.connect(method = 'odbc',authentication = "LDAP",
system=server, username=username, password=password)
# SQL ALCHEMY from teradata
from sqlalchemy import create_engine
user = 'user'
pwd = 'pwd'
host = '1'11.00.00.00'
td_engine = create_engine('teradata://'+user+':'+pwd+'#'+host+':22/' )
result = td_engine.execute('select top 100 * from temp.sampledata')
# USING PYODBC: the below code gave me a new error saying ('01000', "
[01000]
[unixODBC][Driver Manager]Can't open lib 'Teradata' : file not found
(0) (SQLDriverConnect)")
import pyodbc
td_conn = pyodbc.connect('DRIVER= .
{Teradata};DBCName='+server+';UID='+username+';PWD='+ password,
automcommit=True)
cursor = td_conn.cursor()
Regardless, I was unable to made a connection to teradata, could someone let me know what's going on here and how to fix this issue once for all.
Thanks!
Found the answer using pyodbc module. Replaced Driver = {Teradata} parameter with full path where the driver is located, check below fpr the full connection string. Please note that this can only be used on MAC OS.
td_conn = pyodbc.connect('DRIVER={/Library/Application Support/teradata/client/16.20/lib/tdataodbc_sbu.dylib};DBCName='+server+';UID='+username+';PWD='+ password, automcommit=True, authentication = "LDAP")
Anyone knows How to make connection in python to connect as400 iseries system and call any as400 programs with parameter.
For example how to create library by connecting as400 through python. I want to call " CRTLIB LIB(TEST) " from python script.
I am able to connect to DB2 database through pyodbc package.
Here is my code to connect DB2 database.
import pyodbc
connection = pyodbc.connect(
driver='{iSeries Access ODBC Driver}',
system='ip/hostname',
uid='username',
pwd='password')
c1 = connection.cursor()
c1.execute('select * from libname.filename')
for row in c1:
print (row)
If your IBM i is set up to allow it, you can call the QCMDEXC stored procedure using CALL in your SQL. For example,
c1.execute("call qcmdexc('crtlib lib(test)')")
The QCMDEXC stored procedure lives in QSYS2 (the actual program object is QSYS2/QCMDEXC1) and does much the same as the familiar program of the same name that lives in QSYS, but the stored procedure is specifically meant to be called via SQL.
Of course, for this example to work, your connection profile has to have the proper authority to create libraries.
It's also possible that your IBM i isn't set up to allow this. I don't know exactly what goes into enabling this functionality, but where I work, we have one partition where the example shown above completes normally, and another partition where I get this instead:
pyodbc.Error: ('HY000', '[HY000] [IBM][System i Access ODBC Driver][DB2 for i5/OS]SQL0901 - SQL system error. (-901) (SQLExecDirectW)')
This gist shows how to connect to an AS/400 via pyodbc:
https://gist.github.com/BietteMaxime/6cfd5b2dc2624c094575
A few notes; in this example, SYSTEM is the DSN you're set up for the AS/400 in the with pyodbc.connect statement. You could also switch this to be SERVER and PORT with these modifications:
import pyodbc
class CommitMode:
NONE = 0 # Commit immediate (*NONE) --> QSQCLIPKGN
CS = 1 # Read committed (*CS) --> QSQCLIPKGS
CHG = 2 # Read uncommitted (*CHG) --> QSQCLIPKGC
ALL = 3 # Repeatable read (*ALL) --> QSQCLIPKGA
RR = 4 # Serializable (*RR) --> QSQCLIPKGL
class ConnectionType:
READ_WRITE = 0 # Read/Write (all SQL statements allowed)
READ_CALL = 1 # Read/Call (SELECT and CALL statements allowed)
READ_ONLY = 2 # Read-only (SELECT statements only)
def connstr(server, port, commit_mode=None, connection_type=None):
_connstr = 'DRIVER=iSeries Access ODBC Driver;SERVER={server};PORT={port};SIGNON=4;CCSID=1208;TRANSLATE=1;'.format(
server=server,
port=port,
)
if commit_mode is not None:
_connstr = _connstr + 'CommitMode=' + str(commit_mode) + ';'
if connection_type is not None:
_connstr = _connstr + 'ConnectionType=' + str(connection_type) + ';'
return _connstr
def main():
with pyodbc.connect(connstr('myas400.server.com', '8471', CommitMode.CHG, ConnectionType.READ_ONLY)) as db:
cursor = db.cursor()
cursor.execute(
"""
SELECT * FROM IASP.LIB.FILE
"""
)
for row in cursor:
print(' '.join(map(str, row)))
if __name__ == '__main__':
main()
I cleaned up some PEP-8 as well. Good luck!