I know that I need to "import MySQLdb" to connect to a MySQL database. But what is the name of the library that we need to import when we are using "cleardb mysql"?
I am hard coding as below to connect, but I guess due to wrong library, I am getting errors. Below are my points to explain my situation::
1) I have installed "MySQldb", and imported it through import keyword.
2) when I use port number in the connectivity syntax, I got "TypeError: an integer is required".
db = MySQLdb.connect("server_IP",3306,"uid","pwd","db_name")
so I removed the port number
import MySQLdb
db = MySQLdb.connect("server_IP","uid","pwd","db_name")
cur = db.cursor()
and the error vanishes. Is that the right method?
3) Everything goes fine until I execute the function "curson.execution("SELECT VERSION()")" to execute sql queries.
curson.execution("SELECT VERSION()")
I am getting error as below
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
cursor.execute("use d_7fc249f763d6fc2")
File "path_to\cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "path_to\connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
OperationalError: (2006, 'MySQL server has gone away')
So, is this happening due to library that I have imported? or, if the library is correct, then what seems to be the issue?
The port number is the fifth positional argument, not the second.
db = MySQLdb.connect("server_IP", "uid", "pwd", "db_name", 3306)
Related
I'm upgrading a database application from Python 2.7 to 3.4. I originally used mysqldb, but I'm trying to switch to mysql-connector. My database connection fails with an internal TypeError. Here's the code and the error:
import mysql.connector
try:
dbh = mysql.connector.connect("localhost","pyuser","pypwd","jukebox")
except mysql.connector.Error as err:
print("Failed opening database: {}".format(err))
exit(1)
and here's the error:
# python loadcd.py
Traceback (most recent call last):
File "loadcd.py", line 12, in <module>
dbh = mysql.connector.connect("127.0.0.1","pyuser","pypwd","jukebox")
File "/usr/local/lib/python3.4/dist-packages/mysql/connector/__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/mysql/connector/connection.py", line 57, in __init__
super(MySQLConnection, self).__init__(*args, **kwargs)
TypeError: __init__() takes 1 positional argument but 5 were given
I'm at a loss. The exact same connection works with mysqldb, and I can connect with the same credentials using PHP or at the command line.
You should provide keyword arguments:
dbh = mysql.connector.connect(host="localhost",user="pyuser",password="pypwd",database="jukebox")
Use this and replace the values as per your configuration:
import mysql.connector
mydb = mysql.connector.connect(host="localhost",
user="yourusername",
password="yourpassword")
print(mydb)
connstr = """Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;DataSource=first.sdf;"""
conn = adodbapi.connect(connstr)
cur = conn.cursor()
getresult="select * from ft"
cur.execute(getresult)
result=cur.fetchall()
How can i solve the following error?
Traceback (most recent call last):
File "e:\python1\sqlcompactdb\compact.py", line 7, in <module>
connection = adodbapi.connect(connection_string)
File "C:\Users\khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\adodbapi\adodbapi.py", line 116, in connect
raise api.OperationalError(e, message)
adodbapi.apibase.OperationalError: (InterfaceError("Windows COM Error: Dispatch('ADODB.Connection') failed.",), 'Error opening connection to "Provider=Microsoft.SQLSERVER.CE.OLEDB.4.0; Data Source=E:\\python1\\sqlcompact\\first.sdf;"')
As the error implies, this issue stems from an error when the module tries to make an ADO database connection.
Specifically, when the following code executes
pythoncom.CoInitialize()
c = win32com.client.Dispatch('ADODB.Connection')
This is most likely due to hardware issues like the lack of the correct provider for the needed connection.
Solutions to a similar problem can be found at Connecting to SQLServer 2005 with adodbapi
I have 2 problems with my procedure.
First problem is the procedure doesn't work from python and I don't know why.
Here is my python code:
db = MySQLdb.connect(host="***", # your host, usually localhost
user="***", # your username
passwd="***", # your password
db="***") # name of the data base
# you must create a Cursor object. It will let
# you execute all the queries you need
cursor = db.cursor()
args=('SugarSHape',10024,241)
cursor.callproc('p_add_fb_data', args)
My procedure:
CREATE DEFINER=`leverate`#`%` PROCEDURE `p_add_fb_data`(IN p_username VARCHAR(45),IN p_impressions INT(45),IN p_linkClicks INT(45))
BEGIN
DECLARE id INT(5);
SELECT id_user FROM t_user WHERE t_user.username=p_username;
INSERT INTO t_fb_data(Impressions,LinkClicks)
Values(p_impressions, p_linkClicks);
END
I know that id is useless, but I need it later.
If I start the procedure from workbench it works. But if my Python script starts I don't get a new line in my table.
My second problem is if I start the procedure some more time I got this error:
> Traceback (most recent call last): File "C:\Python27\Leverate.py",
> line 76, in <module>
> cursor.execute("SELECT * FROM t_user") File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 174, in
> execute
> self.errorhandler(self, exc, value) File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in
> defaulterrorhandler
> raise errorclass, errorvalue ProgrammingError: (2014, "Commands out of sync; you can't run this command now")
My code for this problem:
for z in range(0,5):
args=(Clients[z][1],data["results"][z]["impressions"],data["results"][z]["clicks"]);
cursor.callproc('p_add_fb_data', args)
I hope it's not too much and that someone can help me.
I keep getting the same error everytime I try to INSERT data into a MySQL table in my Python script:
tools.cerabot#tools-login:~/wikitool-tasks2$ python didyouknow.py
didyouknow.py:62: Warning: Table 'did_you_know' already exists
self.cursor.execute(self.create_query)
Traceback (most recent call last):
File "didyouknow.py", line 121, in <module>
test._parse_page()
File "didyouknow.py", line 109, in _parse_page
self.cursor.execute(record_exists.format(item["name"]))
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':Did you know nominations/Cirrus (song)' at line 1")
The code for this is openly viewable on GitHub, though you'd be particularly interested in lines 95 through 116. I've tried escaping and unicoding the string, modifying my query, nothing. (Admittedly, I'm a basic MySQL programmer.) Could anyone experienced in the area help me figure this out please?
The problem is that the "funny characters" in your Wikipedia titles are messing with the SQL syntax. You need to handle that. This can be done by escaping, but the best practice is to use SQL parameterization, like so:
record_exists = u"SELECT COUNT(*) FROM did_you_know WHERE " \
"name = %s"
# and later on
self.cursor.execute(record_exists, item["name"])
You're actually already doing this later on (line 112).
I have searched high and low on this site and many others and have found similar questions, but none of the answers have worked for me (usually just accounting for the tuple). I'm writing a python script to parse html pages and populate a database. I have almost everything working except the populating part...
Here is the code segment that deals with the mySQL database (note: using MySQLdb module in python)
conn = MySQLdb.connect(user="root", passwd="xxxxx",db="nutrients")
cur = conn.cursor()
test = "Canned Corn"
cur.execute("INSERT INTO food (name) VALUES (%s)", (test,))
conn.commit()
I was first testing it with parsed string but that wasn't working. This gives me 2 errors:
Traceback (most recent call last):
File "C:\Python32\lib\site-packages\MySQLdb\cursors.py", line 171, in execute
r = self._query(query)
File "C:\Python32\lib\site-packages\MySQLdb\cursors.py", line 330, in _query
rowcount = self._do_query(q)
File "C:\Python32\lib\site-packages\MySQLdb\cursors.py", line 294, in _do_query
db.query(q)
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax
; check the manual that corresponds to your MySQL server version for the right s
yntax to use near '%s)' at line 1")
Traceback (most recent call last):
File "C:\Python32\lib\site-packages\MySQLdb\cursors.py", line 171, in execute
r = self._query(query)
File "C:\Python32\lib\site-packages\MySQLdb\cursors.py", line 330, in _query
rowcount = self._do_query(q)
File "C:\Python32\lib\site-packages\MySQLdb\cursors.py", line 294, in _do_query
db.query(q)
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax
; check the manual that corresponds to your MySQL server version for the right s
yntax to use near '%s)' at line 1")
Why does this query not work!?
Update: After nearly pulling my hair out I decided to revert back to 2.7.3 and guess what... everything works now :D Should have know it was an error of that type... Thanks for the help none the less everyone!
I just ran across this too. After a lot of digging around, this was found to work (note the change to values):
conn = MySQLdb.connect(user="root", passwd="xxxxx",db="nutrients")
cur = conn.cursor()
test = "Canned Corn"
cur.execute("INSERT INTO food (name) VALUES ({0})", (test,))
conn.commit()
Background info: In the port of MySQLdb to Python 3 posted on an unofficial Python packages site, the execute function in cursors.py was modified to use format() rather than the % operator. Hence why %s remains in the SQL statement rather than being substituted. It looks like these changes were never upstreamed to the official source.
I think you don't need any tricky or advanced SQL; you just need to store and retrieve some stuff. So, I think an ORM might make life a lot easier for you.
I suggest you try using the Autumn ORM:
http://pypi.python.org/pypi/autumn/0.5.1
That is a small and simple ORM, easy to understand and work with.
Another good and popular ORM for Python is SQLAlchemy:
http://www.sqlalchemy.org/
You should write your code like this :
conn = MySQLdb.connect(user="root", passwd="xxxxx",db="nutrients")
cur = conn.cursor()
test = "Canned Corn"
cur.execute("INSERT INTO food (name) VALUES (%s)" % (test,) )
conn.commit()
If you are new to python and have some programming experience , then follow Dive Into Python book. Its free.