I am currently trying to send data to my database that is hosted on the web using python. There are no MySql errors being thrown however running in ninja I get this output:
Traceback (most recent call last):
File "/home/nekasus/tryme.py", line 8, in <module>
port = "3306") # name of the data base
File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
TypeError: an integer is required
This is my code:
import fakevalues
import MySQLdb
fakevalues.randomnumber()
db = MySQLdb.connect(host="sql8.freemysqlhosting.net",
user="sql8167767",
passwd="placeholder",
db="sql8167767",
port = "3306")
cur = db.cursor()
try:
cur.execute("INSERT INTO hydroponics (ph, electricalconductivity, nutrientsolutiontemperature, nutrientsolutiondepth) VALUES (1, 1, 1, 1)")
except MySQLdb.Error as e:
print(e)
db.close()
You are missing the commit.
db.commit()
port needs to be an integer, not a string.
Related
import mysql.connector
conn = mysql.connector.connect(host="localhost",
user="root",
password="mypassword",
database="mydatabase"
)
conn = mysql.connector.connect('tablename.frm')
conn.close()
When i run this program, the following error occurs:-
Traceback (most recent call last):
File "C:\Users\Real Project MYSQL.py", line 3, in <module>
conn = mysql.connector.connect('PAYSLIP.db')
File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\__init__.py", line 265, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Users\AppData\Local\Programs\Python\Python38-32\lib\site-packages\mysql\connector\connection.py", line 64, in __init__
super(MySQLConnection, self).__init__(*args, **kwargs)
TypeError: __init__() takes 1 positional argument but 2 were given
>>>
#I am unable to solve this error.
fixed: just using mysql.connector package now.
i am a few programming with python now and i wanted to create a use login/logout system with a database linked to a self created web platform for managment, logging, etc...
now i wanted to perform a query to get all users from my database but for some reason im not able to get any results i tried:
# as requested, connector method.
def initiate_connection(self):
return MySQLdb.connect("localhost", "root", "", "tester")
# This works !
def get_database_version(self):
db = self.initiate_connection() # Instantiate db connection
curs = db.cursor() # Server sided cursors - ref more info: https://mysqlclient.readthedocs.io/user_guide.html#cursor-objects
curs.execute("SELECT VERSION();")# Query command
data = curs.fetchone() # Fetch result.
db.close() # Close conn
return data
# This doesnt? :(
def get_users(self):
db = self.initiate_connection() # Instantiate db connection
curs = db.cursor() # Server sided cursors - ref more info: https://mysqlclient.readthedocs.io/user_guide.html#cursor-objects
curs.execute("SELECT name FROM users")# Query command
data = curs.fetchone() # Fetch result.
db.close() # Close conn
return data
But i get an uknown column error, so i tried selecting everything to see what i get from that result: Nonetype, Also ! i am able to retrieve version from database so i assume im connected properly.
Im pretty clueless in what im doing wrong here any ideas?
Also db structure is:
db->tester
table->users
- id
- name
- password
- salt
- email
Edit:
Actual error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "D:\Programmas\PyCharm Community Edition 2019.2.5\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "D:\Programmas\PyCharm Community Edition 2019.2.5\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/oj/PycharmProjects/RegisteryHandeler/DatabaseHandler.py", line 25, in <module>
print(database_handler().get_users())
File "C:/Users/oj/PycharmProjects/RegisteryHandeler/DatabaseHandler.py", line 18, in get_users
curs.execute("SELECT name FROM users")# Query command
File "C:\Users\oj\PycharmProjects\RegisteryHandeler\venv\lib\site-packages\MySQLdb\cursors.py", line 209, in execute
res = self._query(query)
File "C:\Users\oj\PycharmProjects\RegisteryHandeler\venv\lib\site-packages\MySQLdb\cursors.py", line 315, in _query
db.query(q)
File "C:\Users\oj\PycharmProjects\RegisteryHandeler\venv\lib\site-packages\MySQLdb\connections.py", line 239, in query
_mysql.connection.query(self, query)
MySQLdb._exceptions.OperationalError: (1054, "Unknown column 'name' in 'field list'")
When in the Python shell I type:
import MySQLdb
Con = MySQLdb.Connect(user='testuser', password='password', db='test')
And I get this:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Nickey\AppData\Local\Programs\Python\Python36\lib\site-packages\MySQLdb\__init__.py", line 86, in Connect
return Connection(*args, **kwargs)
File "C:\Users\Nickey\AppData\Local\Programs\Python\Python36\lib\site-packages\MySQLdb\connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2059, <NULL>)
And no description of the error at all. Please, I am waiting for help. Thanks.
UPDATE: In case somebody will be having this problem, it appears to be connected with the new mysql_auth_plugin, with its new Strong Password Encryption for Authentication. I just switched to old_password_plugin(Legacy Authentication Method) in the configuration of MySQL Server and now [All works!].
According to the docs, password is not a keyword argument like you entered, instead it's passwd as you can see --> here
When I run my script it won't connect to mysql database, I can't find my host name from godaddy I just tried mysq.mydomain.com and mydomain.com. the login details for the database are correct.
Traceback (most recent call last):
File "mysql.py", line 6, in <module>
conn = MySQLdb.connect(host="?",user="?",passwd="?",db="?")
File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1045, "Access denied for user 'official_autotes'#'104.156.228.189' (using password: YES)")
------------------
my code
from threading import Thread
import urllib
import re
import MySQLdb
conn = MySQLdb.connect(host="?",user="?",passwd="?",db="?")
query = "INSERT INTO tutorial (symbol) values ('AAPL')"
x = conn.cursor()
x.execute(query)
row = x.fetchal()
Grand privilage to the user and ip trying to access the MySQL table using the Python code. To do the same, execute the following commands in the mysql shell.
GRANT <privileges> ON database.* TO 'user'#'ip' IDENTIFIED BY 'password';
flush privileges;
I have a crawler written in Python, which can't connect to the database after crawling for a while. Sometimes it works for 10 minutes, after which the following error log appears:
(2003, "Can't connect to MySQL server on 'localhost' (10055)")
(2003, "Can't connect to MySQL server on 'localhost' (10055)")
Traceback (most recent call last):
File "C:\Users\Admin\Documents\eclipse\workspace\Crawler\src\Crawlers\Zanox.py", line 73, in <module>
c.main()
File "C:\Users\Admin\Documents\eclipse\workspace\Crawler\src\Crawlers\Zanox.py", line 38, in main
self.getInfo()
File "C:\Users\Admin\Documents\eclipse\workspace\Crawler\src\Crawlers\Zanox.py", line 69, in getInfo
comparator.main()
File "C:\Users\Admin\Documents\eclipse\workspace\Crawler\src\CrawlerHelpScripts\Comparator.py", line 23, in main
self.compare()
File "C:\Users\Admin\Documents\eclipse\workspace\Crawler\src\CrawlerHelpScripts\Comparator.py", line 36, in compare
deliveryInfo = self.db.getDeliveryInfo()
File "C:\Users\Admin\Documents\eclipse\workspace\Crawler\src\Database\dell.py", line 29, in getDeliveryInfo
result = self.db.select(com, vals)
File "C:\Users\Admin\Documents\eclipse\workspace\Crawler\src\Database\Database.py", line 24, in select
self.con.close()
_mysql_exceptions.ProgrammingError: closing a closed connection
So at a certain point it just can't connect to the database, running on localhost, and afterwards produces a ProgrammingError. Handling this exception is not the problem, because it will keep running but also will keep producing the Can't connect Error.
Here is the code I use for inserting/selecting in my database:
def select(self, com, vals):
try:
self.con = mdb.connect(self.host, self.user, self.password, self.database)
cur = self.con.cursor()
cur.execute(com,vals)
ver = cur.fetchall()
return ver
except mdb.Error as e:
print e
finally:
if self.con:
self.con.close()
def insert(self, com, vals):
try:
self.con = mdb.connect(self.host, self.user, self.password, self.database)
cur = self.con.cursor()
cur.execute(com, vals)
self.con.commit()
except mdb.Error as e:
print e
finally:
if self.con:
self.con.close()
Also, the crawler is NOT multi-threaded. Any ideas why it keeps losing connection?
EDIT: It seems that the crawler works fine untill it has inserted about 15.000 records in the database. If the table holds about 15.000 records, the crawler produces the error a lot quicker.