I want to export MYSQLdb tables into .csv format.
I tried this:
connection = MySQLdb.connect(host='localhost',
user='***',
passwd='***',
db='database1',
use_unicode=True,
charset="utf8")
cursor = connection.cursor()
query = """ select *
from example_table1
into outfile 'MYFOLDER'
fields terminated by ';'
enclosed by '"'
lines terminated by '';
"""
cursor.execute(query)
connection.commit()
cursor.close()
I get this error message:
Traceback (most recent call last):
File "mysql_export_to_csv.py", line 46, in <module>
cursor.execute(query)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 226, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorvalue
_mysql_exceptions.InternalError: (1, 'Can\'t create/write to file \'/usr/src/Python-2.7.13/output.csv\' (Errcode: 30 "Read-only file system")')
What is the problem in this code? Why I can't export it to .csv?
I suggest to try saving your in a directory where you surely have write permissions like /tmp/
Like this:
connection = MySQLdb.connect(host='localhost',
user='***',
passwd='***',
db='database1',
use_unicode=True,
charset="utf8")
cursor = connection.cursor()
query = """ select *
from example_table1
into outfile '/tmp/myfile.csv'
fields terminated by ';'
enclosed by '"'
lines terminated by '';
Related
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'")
I trying to import my csv file into database. but it fails.
# -*- coding: utf-8 -*-
import MySQLdb
class Database:
def __init__(self):
self.host = 'localhost'
self.user = 'root'
self.port = 3306
self.password = 'root'
self.db = 'test'
self.connection = MySQLdb.connect(self.host, self.user, self.password, self.db, self.port, local_infile = 1)
self.cursor = self.connection.cursor()
def insert_csv_test(self):
query = "LOAD DATA LOCAL INFILE ‘/Users/ankr/Desktop/output’ INTO TABLE details FIELDS TERMINATED BY ‘,’ LINES TERMINATED BY ‘\n’"
self.cursor.execute(query)
self.connection.commit()
self.connection.close()
print("Done")
def close_connection(self):
self.connection.close()
database = Database()
database.__init__()
database.insert_csv_test()
database.close_connection()
It fails. Seeing this below.
Traceback (most recent call last): File "test.py", line 30, in
database.insert_csv_test() File "test.py", line 20, in insert_csv_test
self.cursor.execute(query) File "/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.12-intel.egg/MySQLdb/cursors.py",
line 202, in execute
self.errorhandler(self, exc, value) File "/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.12-intel.egg/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 MySQL server
version for the right syntax to use near
'\xe2\x80\x98/Users/ankr/Desktop/output\xe2\x80\x99 INTO TABLE details
FIELDS TERMINATED BY \xe2\x80\x98,\xe2\x80\x99 LI' at line 1")
Any Help would be appreciated.
This may be a little naive answer, but I think the problem lies in ‘ character. It's getting interpreted as a UTF-8 character. Try replacing it with a regular single quote - ' .
Looks like you at least have a problem in the call. You are connecting to the database twice:
database = Database()
database.__init__()
You should just run:
database = Database()
You should be using \' inside the SQL query (not ') since you want to avoid them being directly interpreted as mentioned already in another comment.
Below are the code that is not working
#!/usr/bin/python
import urllib
import datetime
import mysql.connector
data['price'] = 100
# Write to database
cnx = mysql.connector.connect(user='user', password='pw', database='db')
cursor = cnx.cursor()
query = ("INSERT INTO records(price) VALUES(%s) ")
cursor.execute(query, (data['price']))
cnx.commit()
cursor.close()
cnx.close()
MySQL has the corresponding table and column.
id (PRIMARY INT AUTO_INCREMENT)
price (FLOAT)
There is no connection problem.
It gives the following error. Any idea?
Traceback (most recent call last): File "./sys-fetchdata.py", line
22, in
cursor.execute(query, (data['price'])) File "/usr/lib/python2.7/dist-packages/mysql/connector/cursor.py", line
507, in execute
self._handle_result(self._connection.cmd_query(stmt)) File "/usr/lib/python2.7/dist-packages/mysql/connector/connection.py", line
722, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query)) File
"/usr/lib/python2.7/dist-packages/mysql/connector/connection.py", line
640, in _handle_result
raise errors.get_exception(packet) mysql.connector.errors.ProgrammingError: 1064 (42000): You have an
error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near '%s)' at line 1
the MySQLCursor.execute() method takes a tuple (or a dict) as second (params) argument.
in your case: it should be (data['price'], ) (note the additional comma; without it the brackets have no effect) instead of (data['price']).
Let's try to find a solution:
Do not execute your query, instead mogrify and print it. For example:
query = ("INSERT INTO records(price) VALUES(%s) ")
query_to_be_executed = cursor.mogrify(query, (data['price']))
print(query_to_be_executed)
Next try to execute your printed query manually in bash or phpMyAdmin and fix it.
Finally use fixed query in your python code.
Trying to query for dates in an Access DB, and it keeps returning the follow error:
Traceback (most recent call last):
File "C:/Users/sniederriter/Desktop/SATG.py", line 10, in <module>
for row in cursor.execute(SQL):
File "C:\Python34\lib\site-packages\pypyodbc-1.3.3-py3.4.egg\pypyodbc.py", line 1605, in execute
self.execdirect(query_string)
File "C:\Python34\lib\site-packages\pypyodbc-1.3.3-py3.4.egg\pypyodbc.py", line 1631, in execdirect
check_success(self, ret)
File "C:\Python34\lib\site-packages\pypyodbc-1.3.3-py3.4.egg\pypyodbc.py", line 986, in check_success
ctrl_err(SQL_HANDLE_STMT, ODBC_obj.stmt_h, ret, ODBC_obj.ansi)
File "C:\Python34\lib\site-packages\pypyodbc-1.3.3-py3.4.egg\pypyodbc.py", line 954, in ctrl_err
raise ProgrammingError(state,err_text)
pypyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'Date *'.")
Here is my code:
import pypyodbc
DBfile = (r'C:\Users\sniederriter\Desktop\SATGexpenses.accdb')
conn = pypyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ='+DBfile)
cursor = conn.cursor()
print(DBfile)
SQL = 'SELECT Date * FROM 20142015;'
for row in cursor.execute(SQL):
print (row.Date)
cursor.close()
conn.close()
Your MySQL query is wrong. Following code selects all fields from the table:
SELECT * FROM Table
If you want specific fields write:
SELECT field1, field2, field3 FROM table
But only do one of the things. Either all or specified fields.
I am trying to insert some data into a MySQL DB with Python and MySQLdb. When i do the following:
query = "INSERT INTO universitats (universitat) VALUES ('%s')" % (lloc)
cursor.execute(query)
db.commit()
I get this error:
Traceback (most recent call last):
File "read.py", line 39, in <module>
cursor.execute(query)
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 MySQL server version for the right syntax to use near 'Hospitalet de Llobregat')' at line 1")
What am I doing wrong?
This lines:
query = "INSERT INTO universitats (universitat) VALUES ('%s')" % (lloc)
cursor.execute(query)
should look like this
query = "INSERT INTO universitats (universitat) VALUES (%s)"
cursor.execute(query,(lloc,))
and then commit.