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.
Related
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.
I am trying to use the Cloud Shell to update some user permissions. I am logging in using gcloud sql connect my-instance --user=root
gcloud sql connect my-instance
Whitelisting your IP for incoming connection for 5 minutes...done.
Connecting to database with SQL user [sqlserver].********************************************************************************
Python command will soon point to Python v3.7.3.
Python 2 will be sunsetting on January 1, 2020.
See http://https://www.python.org/doc/sunset-python-2/
Until then, you can continue using Python 2 at /usr/bin/python2, but soon
/usr/bin/python symlink will point to /usr/local/bin/python3.
To suppress this warning, create an empty ~/.cloudshell/no-python-warning file.
The command will automatically proceed in seconds or on any key.
********************************************************************************
> Password:
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/usr/local/lib/python2.7/dist-packages/mssqlcli/main.py", line 117, in <module>
main()
File "/usr/local/lib/python2.7/dist-packages/mssqlcli/main.py", line 110, in main
run_cli_with(mssqlcli_options)
File "/usr/local/lib/python2.7/dist-packages/mssqlcli/main.py", line 43, in run_cli_with
from mssqlcli.mssql_cli import MssqlCli
File "/usr/local/lib/python2.7/dist-packages/mssqlcli/mssql_cli.py", line 18, in <module>
from prompt_toolkit.shortcuts import PromptSession, CompleteStyle
ImportError: cannot import name PromptSession
A) I have made the root user's password so insecure and easy there is no way I am mistyping it.
B) It is the third of January, so I really don't know what this Python version error is on about. I made the file but FYI ~/.cloudshell did not exist so I had to make it first. Even so, it just suppresses the version warning, the main error persists when I try to log in.
The documentation acknowledges there are a couple other login methods using glcoud beta sql connect, but that gets me another error
2020/01/04 18:38:41 Rlimits for file descriptors set to {&{8500 1048576}}
2020/01/04 18:38:41 invalid json file "/tmp/tmp.s38C662KKr/legacy_credentials/me#gmail.com/adc.json": open /tmp/tmp.s38C662KKr/legacy_credentials/me#gmail.com/adc.json: no such file or directory
ERROR: (gcloud.beta.sql.connect) Failed to start the Cloud SQL Proxy.
Same for alpha.
This is the first thing I have typed into Cloud Shell, so I can't imagine what could have broken PromptSession.
How can I resolve this error and log into SQL Server using Cloud Shell?
There most likely is an issue while attempting to connect from the Cloud Shell (I managed to connect from a Compute Engine instance with this command); possibly related to the Python run-time / environment variable. It has been reported here. Engineering is aware and are looking into it.
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
Suppose Hive is installed in say, "g" cluster. I do not have access to the Gold Cluster. I'm doing my python development work in "s" cluster. I can access Hive from the "s" cluster and run queries.
I have the below code to connect to Hive from a Python script running in "s" cluster.
some_table is a table that already exists in Hive. I would like to execute a simple select * from some_table command to get some results.
import sys
sys.path.append("/usr/lib/hive/lib/py")
from hive_service import ThriftHive
from hive_service.ttypes import HiveServerException
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
transport = TSocket.TSocket('what-ever-server', what-ever-port)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = ThriftHive.Client(protocol)
transport.open()
print "connect success"
client.execute("SELECT * FROM some_table")
print client.fetchAll()
print "executed"
But I get the below error after "connect success" is printed. I am assuming that the connection was successful.
Traceback (most recent call last):
File "hiveConnect.py", line 30, in <module>
row = client.execute("SELECT * FROM some_table")
File "/usr/lib/hive/lib/py/hive_service/ThriftHive.py", line 68, in execute
self.recv_execute()
File "/usr/lib/hive/lib/py/hive_service/ThriftHive.py", line 79, in recv_execute
(fname, mtype, rseqid) = self._iprot.readMessageBegin()
File "/usr/lib/hive/lib/py/thrift/protocol/TBinaryProtocol.py", line 137, in readMessageBegin
name = self.trans.readAll(sz)
File "/usr/lib/hive/lib/py/thrift/transport/TTransport.py", line 58, in readAll
chunk = self.read(sz-have)
File "/usr/lib/hive/lib/py/thrift/transport/TTransport.py", line 155, in read
self.__rbuf = StringIO(self.__trans.read(max(sz, self.DEFAULT_BUFFER)))
File "/usr/lib/hive/lib/py/thrift/transport/TSocket.py", line 94, in read
raise TTransportException('TSocket read 0 bytes')
thrift.transport.TTransport.TTransportException: None
What am I doing wrong in this code? I am not experiencing any error while connecting to Hive using the server-name and port, so I'm assuming everything is fine there and that the connection to Hive is not the issue.
Well after a bit more research I found that it was actually HiveServer2 that was being used with a port number of 10,000. After that I had to install pyhs2 for it to work properly.
I'm afraid this might is just an configuration issue but maybe also some issue with coding. However, I'm trying to connect with a database using Python and sqlanydb. SQLAnywhere is installed within /opt/sybase and Shell is sourcing the sa_config.sh file.
Connection with normal tools are working well: E.g. via
dbisql -c "uid=dba;pwd=sql;eng=mydemo"
However, when I try doing this in Python with
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ctypes
import sqlanydb
def main(args):
db1 = sqlanydb.connect( userid="DBA",
password="sql",
DataSourceName="mytest")
db1.close
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
it's failing with
Traceback (most recent call last):
File "./myfile.py", line 52, in <module>
sys.exit(main(sys.argv))
File "./myfile.py", line 42, in main
DataSourceName="mytest")
File "/usr/local/lib/python2.7/dist-packages/sqlanydb.py", line 461, in connect
return Connection(args, kwargs)
File "/usr/local/lib/python2.7/dist-packages/sqlanydb.py", line 510, in __init__
self.handleerror(*error)
File "/usr/local/lib/python2.7/dist-packages/sqlanydb.py", line 520, in handleerror
eh(self, None, errorclass, errorvalue)
File "/usr/local/lib/python2.7/dist-packages/sqlanydb.py", line 342, in standardErrorHandler
raise errorclass(errorvalue)
sqlanydb.OperationalError: Parse error: Cannot find .ini file
I had a look onto sources of sqlanydb but didn't find any reference to an .ini file looking for at this point. So what is the correct way of enabling the ASA16 to run with sqlanydb?
That's the error you'll get if you have no ~/.odbc.ini file. You are specifying "DataSourceName=mytest", which tells the SQL Anywhere ODBC driver to look in ~/.odbc.ini for a DSN called mytest, but no .ini file was found.
Use the dbdsn utility to create the DSN in question.
Full disclosure: I work for SAP in SQL Anywhere engineering.