Python cannot connect to DB on XAMPP - python

I got a python file from another pc, and that pc is using mySQL to be the db. Now I'm trying to use db on XAMPP, but it seems some errors happened. I tried to use Anaconda Prompt to check the problem that shows below:
Traceback (most recent call last):
File "C:\xampp\htdocs\distest.py", line 48, in <module(ignore me)>
conn = pymysql.connect(host='localhost',
File "C:\Users\user\anaconda3\lib\site-packages\pymysql\connections.py", line 311, in init
self.encoding = charset_by_name(self.charset).encoding
AttributeError: 'NoneType' object has no attribute 'encoding'
I've imported something about connection between python and mySQL:
import pymysql
and this is line 48 in python file:
conn = pymysql.connect(host='localhost',
user='root',
passwd='',
db='test1',
charset='utf8_general_ci',
cursorclass = pymysql.cursors.SSCursor)
How can I fix it?

You cannot connect as root. You must use a different account.
Ideally you would create an account for the application with just the permissions it needs, as this is best practice.

Related

Python and SQL Anywhere 17 trouble with "Could not load dbcapi"

I installed SQL Anywhere 17 client. I ran /opt/sqlanywhere17/bin64/sa_config.sh And still have problem with sqlanydb.InterfaceError: ('Could not load dbcapi. Tried:None,dbcapi.dll,libdbcapi_r.so,libdbcapi_r.dylib', 0)
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
import ctypes
import sqlanydb
conn = sqlanydb.connect(UserID='XXX', Password='XXX', Host='10.10.10.10', DatabaseName='XXX', ServerName='XXX')
curs = conn.cursor()
curs.close()
conn.close()
Log:
Traceback (most recent call last):
File "./systest.py", line 6, in <module>
conn = sqlanydb.connect(UserID='XXX', Password='XXX', Host='10.10.10.10', DatabaseName='XXX', ServerName='XXX')
File "/usr/local/lib/python3.6/site-packages/sqlanydb.py", line 522, in connect
return Connection(args, kwargs)
File "/usr/local/lib/python3.6/site-packages/sqlanydb.py", line 538, in __init__
parent = Connection.cls_parent = Root("PYTHON")
File "/usr/local/lib/python3.6/site-packages/sqlanydb.py", line 464, in __init__
'libdbcapi_r.dylib')
File "/usr/local/lib/python3.6/site-packages/sqlanydb.py", line 456, in load_library
raise InterfaceError("Could not load dbcapi. Tried: " + ','.join(map(str, names)))
sqlanydb.InterfaceError: ('Could not load dbcapi. Tried: None,dbcapi.dll,libdbcapi_r.so,libdbcapi_r.dylib', 0)
Thanks for your help in advance
I faced the same issue and took me 2 days to solve it.
Here is the solution, your solution is in point 2.
If running on Linux or one of the Unix platforms, you'll need to set
up your environment before launching Python. So, download the sql
anywhere12 / 17 and install. ( You have done this)
open the terminal and run sa_config{.sh|.csh} script so your Python application can locate the required libraries (including libdbcapi12.dll).
Make sure that you run sa_config.sh file and app.py in same terminal otherwise you will face the same issue.
Example, I have created the bash file which run first.
. /opt/sqlanywhere17/bin32/sa_config.sh
python app.py
source : Full answer can be found here on sqlanywhere forum

No access to database

I am trying to open an FDB file, but it doesn't seem to work. Whatever I try, I get this error:
Opening ./20190401_database.fdb
Traceback (most recent call last):
File "test.py", line 16, in <module>
conn = fdb.connect(dsn=local_copy, user='****', password='****', charset='iso8859_1')
File "/usr/local/lib/python3.6/site-packages/fdb/fbcore.py", line 848, in connect
"Error while connecting to database:")
fdb.fbcore.DatabaseError: ('Error while connecting to database:\n- SQLCODE: -551\n- no permission for read-write access to database /var/www/wsgi/data/20190401_database.fdb', -551, 335544352)
I am running this code as user apache, and the same user just copied the FDB file into that location, so I am pretty sure the user has read/write access.
import fdb
from datetime import date, timedelta
DB_PATH="."
yesterday = date.today() - timedelta(days = 1)
yesterday = yesterday.strftime("%Y%m%d")
filename = f'{yesterday}_database.fdb'
local_fullpath = f'{DB_PATH}/{filename}'
local_copy = local_fullpath
print("Opening "+local_copy)
conn = fdb.connect(dsn=local_copy, user='****', password='****', charset='iso8859_1')
conn.close()
Even when running the script as root, I get the same error.
The problem was in the fact that the FDB file has to be read/writable by the firebird user.
Setting that correctly made it work.

How do I connect to a Netcool / Omnibus "Object Server" using Python?

I'm trying to connect to a Netcool 7.1 Object Server using Python, but I'm running into issues. It seems to be a Sybase type database, but stripped down. I'm using the Sybase module and freetds, but I get the following error when I try to connect:
Traceback (most recent call last):
File "netcool.py", line 12, in <module>
db = Sybase.connect('foo','foo','foo','foo')
File "/usr/lib64/python2.6/site-packages/Sybase.py", line 1194, in connect
datetime, bulkcopy, locale, inputmap, outputmap)
File "/usr/lib64/python2.6/site-packages/Sybase.py", line 850, in __init__
self.connect()
File "/usr/lib64/python2.6/site-packages/Sybase.py", line 898, in connect
status = conn.ct_options(CS_SET, CS_OPT_CHAINXACTS, not self.auto_commit)
File "/usr/lib64/python2.6/site-packages/Sybase.py", line 272, in _servermsg_cb
raise DatabaseError(msg)
Sybase.DatabaseError: Msg 17001, Level 10
No SRV_OPTION handler installed.
Has anyone successfully connected to a Netcool Object Server using Python? Is there any way I can edit the Sybase module to make it work? Worst case scenario, would it be possible to write something that communicates with the nco_sql client? I want the ability to read and write to the DB.
Any help or info would be greatly appreciated.
Eventually I figured this out on my own. If you use the SAP Sybase JDBC drivers (jconn4.jar) in conjunction with the JayDeBeApi module, you can successfully connect and read/write to the object server. This is the only Python solution I could find that works. If anyone else has an different method, feel free to share.

Trouble with Sqlite on Python/Django

I'm trying to learn Django and following along the Django Book tutorial and I'm getting an error when I type these lines into the Python shell:
>>> from django.db import connection
>>> cursor = connection.cursor()
Here's the traceback I get:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python27\lib\site-packages\django\db\backends\__init__.py", line 306,
in cursor
cursor = self.make_debug_cursor(self._cursor())
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py", line
288, in _cursor
self._sqlite_create_connection()
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py", line
278, in _sqlite_create_connection
self.connection = Database.connect(**kwargs)
OperationalError: unable to open database file
Any ideas on how to solve this? On my settings.py I have DATABASES ENGINE set to: django.db.backends.sqlite3 and NAME set to: C:\Python27/PythonProjects/mysite. Thanks!
I find (in modern Django) that just giving a filename like database.sqlite for the filename as the name is the best option. Django should interpret that by sticking the new database in the root folder of the project, which is perfect for my needs.

Connecting to MySQL with Python in Ubuntu (Weird Error)

I'm using ubuntu 10.10, and after making the mistake of installing LAMP, (was unable to connect to a database with it), I came here and read how important it is to use apt-get install python-mysqldb instead. Deleted LAMP, re-installed using apt-get, and am now getting the same error when trying to run a basic server_version.py script.
The script is the server_version.py found here: http://www.kitebird.com/articles/pydbapi.html
My server_version.py script:
# server_version.py - retrieve and display database server version
import MySQLdb
# I have also tried setting host = 'localhost'
conn = MySQLdb.connect (host = "/opt/lampp/var/mysql/mysql.sock",
user = "root",
passwd = "myrealpass",
db = "testdb1")
cursor = conn.cursor ()
cursor.execute ("SELECT VERSION()")
row = cursor.fetchone ()
print "server version:", row[0]
cursor.close ()
conn.close ()
The error is:
Traceback (most recent call last):
File "server_version.py", line 10, in
db = "testdb1")
File "/usr/lib/pymodules/python2.6/MySQLdb/init.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 129, in init
from converters import conversions
File "/usr/lib/pymodules/python2.6/MySQLdb/converters.py", line 165, in
from decimal import Decimal
File "/usr/lib/python2.6/decimal.py", line 137, in
import copy as _copy
File "/home/browning/copy.py", line 4, in
ValueError: need more than 1 value to unpack
Just trying to get some basic experience using databases with python here, so I'm not set on MySQL if there is a better option. I have attempted to re-install mysqldb multiple times using apt-get and pip.
It looks like you have a file named copy.py that is being picked up instead of the Python standard library module copy. Rename or delete your file (and copy.pyc if it was created). Or run from a different directory.

Categories