I am trying to retrieve values from db and I am stack from start because sqlite3 is unable to open the db file after I was giving the right path.
Version 1:
sqlite_file = os.environ.get('DATABASE_URL', 'sqlite:///db.sqlite')
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
Version 2:
sqlite_file = app.config['SQLALCHEMY_DATABASE_URI']
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
Where:
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL', 'sqlite:///db.sqlite')
In both situations the error is
Error on request:
Traceback (most recent call last):
File "/home/ubuntu/workspace/avb/venv/lib/python3.6/site-packages/werkzeug/serving.py", line 209, in run_wsgi
execute(self.server.app)
File "/home/ubuntu/workspace/avb/venv/lib/python3.6/site-packages/werkzeug/serving.py", line 197, in execute
application_iter = app(environ, start_response)
File "/home/ubuntu/workspace/avb/venv/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/ubuntu/workspace/avb/app.py", line 222, in <module>
conn = sqlite3.connect(sqlite_file)
sqlite3.OperationalError: unable to open database file
For sqlite3.connect() start out using the actual filename (e.g. example.db) as the argument.
Same for environ.get(): use the actual filename, not the sqlite:/// address.
Related
I'm trying to connect to mySQL using mysql-connector and I'm getting an error:
runfile('C:/Users/Idan/Desktop/תכנות/ניסויים/idk/adding_test/connect_to_db.py', >wdir='C:/Users/Idan/Desktop/תכנות/ניסויים/idk/adding_test')
Traceback (most recent call last):
File "C:\Users\Idan\Desktop\תכנות\ניסויים\idk\adding_test\connect_to_db.py", >line 13, in
database = 'temp'
File "C:\Users\Idan\anaconda3\lib\site-packages\mysql\connector_init_.py", >line 271, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Users\Idan\anaconda3\lib\site->packages\mysql\connector\connection.py", line 95, in init
self._in_transaction = False
File "C:\Users\Idan\anaconda3\lib\site-packages\mysql\connector\abstracts.py", >line 985, in connect
self._open_connection()
File "C:\Users\Idan\anaconda3\lib\site->packages\mysql\connector\connection.py", line 205, in _open_connection
ssl_options.get('verify_identity') or
File "C:\Users\Idan\anaconda3\lib\site->packages\mysql\connector\connection.py", line 193, in _get_connection
if client_flags & ClientFlag.SSL:
File "C:\Users\Idan\anaconda3\lib\site-packages\mysql\connector\network.py", >line 565, in init
super(MySQLTCPSocket, self).init()
TypeError: super(type, obj): obj must be an instance or subtype of type
I have tried searching online and I can't find problem with my code (and yes the password is correct):
import mysql.connector as mysql
db = mysql.connect(
host = 'localhost',
user = 'root',
password = '10iDun7lEvy3',
database = 'temp'
)
c = db.cursor()
I was trying to connect the mysql with python.
import mysql.connector
db = mysql.connector.connect()
Uptill this point everything was fine. But after this when I started specifying the attributes of the connect command, using the code bellow
import mysql.connector
db = mysql.connector.connect(
host = "localhost", #specifies the host computer(in this case this computer only)
user = "root", #specifies the user
passwd = "<my password was here>"
)
this error came up on running the code,
Traceback (most recent call last):
File "C:/Users/Co-valent Cyclist/Desktop/Python + MySQL/test 1.py", line 6, in <module>
passwd = "co_valent_bonds"
File "C:\Users\Co-valent Cyclist\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\__init__.py", line 177, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Users\Co-valent Cyclist\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 104, in __init__
self.connect(**kwargs)
File "C:\Users\Co-valent Cyclist\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\abstracts.py", line 785, in connect
self._post_connection()
File "C:\Users\Co-valent Cyclist\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\abstracts.py", line 757, in _post_connection
self.set_charset_collation(self._charset_id)
File "C:\Users\Co-valent Cyclist\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\abstracts.py", line 716, in set_charset_collation
charset_name, collation_name))
File "C:\Users\Co-valent Cyclist\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 970, in _execute_query
self.cmd_query(query)
File "C:\Users\Co-valent Cyclist\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 590, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "C:\Users\Co-valent Cyclist\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\connection.py", line 478, in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1115 (42000): Unknown character set: 'utf8mb4'
how do I rectify this? Please help me out. Thanks!!
Use charset="utf8" while creating the connection object .
If your characters actually use the superset utf8mb4 then this may cause problems, but if not, then this should work!
import mysql.connector
db = mysql.connector.connect(
host = "localhost", #specifies the host computer(in this case this computer only)
user = "root", #specifies the user
passwd = "<my password was here>",
charset = "utf8"
)
Here is the code
import pymysql
pymysql.connect(
host='localhost',
port=3306,
user='root',
password='iDontWannaSay',
db='iDontWannaShow',
charset='utf8'
)
and the error Traceback was:
data is :::::b'\xff\x02\x00\xff\x81\x15'....##### I was add near line 1279 which is print("data is :::::%s...."%data[i:i+6])
Traceback (most recent call last):
File "C:\Users\123\Desktop\pymysqldebug.py", line 8, in <module>
charset='utf8'
File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\connections.py", line 709, in __init__
self.connect()
File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\connections.py", line 934, in connect
self._get_server_information()
File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\connections.py", line 1279, in _get_server_information
self.server_charset = charset_by_id(lang).name
File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\charset.py", line 39, in by_id
return self._by_id[id]
KeyError: 255
seems like struct.unpack method parse '\xff\' to 255 and assigned to self.server_language, whatever the non-null charset argument passed.
Is this an MySQL version problem?(version 8.0.1-dmr)
To extend the above link-only accepted answer, consider the following changes on your current pymysql install. With MySQL 8, the mysql-python API does not recognize possibly newer character sets and hence the raised KeyError.
To resolve, locate the connectors.py script under the pymysql module found at this directory:
print(pymysql.__file__)
Backup orginal connectors.py script. Then incorporate the following changes.
Original (lines 1268-1269)
self.server_language = lang
self.server_charset = charset_by_id(lang).name
Replace (lines 1268 - 1272)
self.server_language = lang
try:
self.server_charset = charset_by_id(lang).name
except KeyError:
self.server_charset = None
Be careful to include hidden tabs in indentation with above lines.
Reference
PyMySQL Git fix #591
but where's your variable?
import pymysql
|
v
myVariable = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='iDontWannaSay',
db='iDontWannaShow',
charset='utf8'
)
Here is the code
import pymysql
pymysql.connect(
host='localhost',
port=3306,
user='root',
password='iDontWannaSay',
db='iDontWannaShow',
charset='utf8'
)
and the error Traceback was:
data is :::::b'\xff\x02\x00\xff\x81\x15'....##### I was add near line 1279 which is print("data is :::::%s...."%data[i:i+6])
Traceback (most recent call last):
File "C:\Users\123\Desktop\pymysqldebug.py", line 8, in <module>
charset='utf8'
File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\connections.py", line 709, in __init__
self.connect()
File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\connections.py", line 934, in connect
self._get_server_information()
File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\connections.py", line 1279, in _get_server_information
self.server_charset = charset_by_id(lang).name
File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\charset.py", line 39, in by_id
return self._by_id[id]
KeyError: 255
seems like struct.unpack method parse '\xff\' to 255 and assigned to self.server_language, whatever the non-null charset argument passed.
Is this an MySQL version problem?(version 8.0.1-dmr)
To extend the above link-only accepted answer, consider the following changes on your current pymysql install. With MySQL 8, the mysql-python API does not recognize possibly newer character sets and hence the raised KeyError.
To resolve, locate the connectors.py script under the pymysql module found at this directory:
print(pymysql.__file__)
Backup orginal connectors.py script. Then incorporate the following changes.
Original (lines 1268-1269)
self.server_language = lang
self.server_charset = charset_by_id(lang).name
Replace (lines 1268 - 1272)
self.server_language = lang
try:
self.server_charset = charset_by_id(lang).name
except KeyError:
self.server_charset = None
Be careful to include hidden tabs in indentation with above lines.
Reference
PyMySQL Git fix #591
but where's your variable?
import pymysql
|
v
myVariable = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='iDontWannaSay',
db='iDontWannaShow',
charset='utf8'
)
I am using Python library pymssql to connect to a database. I have never used it before so the error might look trivial.
from os import getenv
import pymssql
server = getenv("192.xxx.xxx.xx")
user = getenv("john.constantine")
password = getenv("xxxxxxx")
conn = pymssql.connect(server, user, password, "tempdb")
cursor = conn.cursor()
cursor.execute('SELECT * from table')
rows = cursor.fetchall()
for row in rows:
print row
conn.close()
I get the following error:
Traceback (most recent call last):
File "C:\Users\Documents\Demo_DB.py", line 8, in <module>
conn = pymssql.connect(server, user, password, "tempdb")
File "pymssql.pyx", line 635, in pymssql.connect (pymssql.c:10734)
File "_mssql.pyx", line 1902, in _mssql.connect (_mssql.c:21821)
File "_mssql.pyx", line 552, in _mssql.MSSQLConnection.__init__ (_mssql.c:5891)
TypeError: argument of type 'NoneType' is not iterable
[Finished in 0.2s with exit code 1]
I connect to DB using these credentials.