I'm trying to get the names of all databases associated with my MySQL server via python (2.7), but am instead just getting the number of databases.
I looked around, and it seems like the only answer may be to use sys for a command line call, get the name of the databases, and proceed from there, but I can't believe that's the only way.
Current Code:
import MySQLdb
serv = MySQLdb.connect(host = "localhost", user = "root", passwd = "abcdefg")
c = serv.cursor()
print c.execute("SHOW DATABASES")
Output:
4
Thanks in advance for the help.
I am not sure if mysql connector is same as your library, but using msyql connector your answer would be something like this:
import mysql.connector
conn = mysql.connector.connect (user='user', password='password',
host='server_address',buffered=True)
cursor = conn.cursor()
databases = ("show databases")
cursor.execute(databases)
for (databases) in cursor:
print databases[0]
Just taking previous comments and reorganicing:
Current Code:
import MySQLdb
serv = MySQLdb.connect(host = "localhost", user = "root", passwd = "abcdefg")
c = serv.cursor()
print c.execute("SHOW DATABASES")
With suggested modifications:
import MySQLdb
serv = MySQLdb.connect(host = "localhost", user = "root", passwd = "abcdefg")
c = serv.cursor()
c.execute("SHOW DATABASES")
l = c.fetchall()
print l
l = [ i[0] for i in l ]
print l
In my case the output looks like (which is the desired output):
(('information_schema',), ('data',), ('mysql',), ('performance_schema',), ('sys',))
['information_schema', 'data', 'mysql', 'performance_schema', 'sys']
Please note the following common error to prevent mistakes: if you try c.execute("SHOW DATABASES").fetchall() you will have an error: AttributeError: 'int' object has no attribute 'fetchall'. This is way you should write firstly c.execute("SHOW DATABASES") and secondly c.fetchall().
Related
I am using python to interact with mysql and when I access columns from mysql i get an output like this:
[('some', 't5vd._kZ'), ('something', 'anything')]
i want it to be:
some, t5vd._kZ
something, anything
my code:
import mysql.connector
mydb = mysql.connector.connect(
host = 'localhost',
user = '####',
password = '######',
database = 'database'
)
cursor = mydb.cursor()
cursor.execute('SELECT * FROM passwords')
result = str(cursor.fetchall())
passwd = ''
print(result)
You can do
for tup in result:
print(*tup)
I am trying to store a pickled nested dictionary in Postgresql (I am aware that this is a quick & dirty method and won't be able to access dictionary contents from Postgresql - usually bad practice)
# boilerplate, preamble and upstream work.
import psycopg2
''' Inputs: nd = dictionary to be pickled '''
pickled = pickle.dumps(nd)
connection = psycopg2.connect(user = "-----",
password = "----",
host = "----",
port = "----",
database = "----")
name = 'database1'
print('Connected...')
cursor = connection.cursor()
print(connection.get_dsn_parameters(),"\n")
cursor.execute("CREATE TABLE thetable (name TEXT, ablob BYTEA)")
print('Created Table...')
cursor.execute("INSERT INTO thetable VALUES(%s)",(psycopg2.Binary(pickled),))
connection.commit()
print('Added Data...')
cursor.close()
connection.close()
print('Connection closed...')
When I come to data data retrieval, I am having many issues importing the data from Postgres - essentially the data is to be opened, unpickled back to the dictionary and visualised. I have tried:
import psycopg2
from io import BytesIO
connection = psycopg2.connect(user = "----",
password = "----",
host = "----",
port = "----",
database = "----")
cursor = connection.cursor()
cursor.execute("SELECT ablob FROM thetable")
result, = cursor.fetchone()
cursor.close()
connection.rollback()
result = BytesIO(result)
print(pickle.load(result))
As per this link: https://www.oreilly.com/library/view/python-cookbook/0596001673/ch08s08.html, and consulted: Insert an image in postgresql database and: saving python object in postgres table with pickle, however have been unable to return the pickled dictionary.
Any advice in achieving this is greatly appreciated!
When your CREATE TABLE lists two fields, you have to list in INSERT which ones you want to fill, unless you fill them all.
import psycopg2
import pickle
dict = {
"foo": "bar"
}
p = pickle.dumps(dict)
connection = psycopg2.connect(database = "test")
cursor = connection.cursor()
cursor.execute("CREATE TABLE thetable (name TEXT, ablob BYTEA)")
cursor.execute("INSERT INTO thetable VALUES(%s,%s)",('test',p))
connection.commit()
cursor.close()
connection.close()
and reading
import psycopg2
import pickle
connection = psycopg2.connect(database = "test")
cursor = connection.cursor()
cursor.execute("SELECT ablob FROM thetable WHERE name='test';")
result = cursor.fetchone()
print pickle.loads(result[0])
cursor.close()
connection.close()
I am trying to connect to mysql database to retrieve some id for some users and use those id to retrieve another set of data from another table. It should be easy but I am getting mysql errors. Here's a snippet of what I am trying to do.
import MySQLdb
from langdetect import detect
my_db = MySQLdb.connect(
host="localhost",
port = 3306,
user="user",
passwd="password",
db="mydb",
charset = 'utf8'
)
sql1 = """SELECT id, comment FROM users WHERE usertype = 5 LIMIT 100"""
users = []
db_cursor = my_db.cursor()
db_cursor.execute(sql1)
users = db_cursor.fetchall()
sql2 = """SELECT first_name, last_name, email FROM user_contact WHERE id = %s"""
user_contact =[]
for user in users:
comment = user[1]
if detect(comment) == 'en':
id = user[0]
db_cursor = my_db.cursor()
db_cursor.execute(sql2, (id))
temp = db_cursor.fetchall()
user_contact . append (temp)
print (user_contact)
This is the error message I get when I try to run this query.
_mysql_exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query')
The first part of the query will normally go through but it usually fails when it tries to connect to mysql again for the second part. I tested with just 100 records just to check if it's an issue with the query running too long but it's still the same even with 10 records.
For your second part, you might not execute sql;)
Try to change
for user in users:
comment = user[1]
if detect(comment) == 'en':
id = user[0]
db_cursor = my_db.cursor()
temp = db_cursor.fetchall()
user_contact . append (temp)
to
for user in users:
comment = user[1]
if detect(comment) == 'en':
id = user[0]
db_cursor = my_db.cursor()
db_cursor.execute(sql1, (id))
temp = db_cursor.fetchall()
user_contact . append (temp)
I try to run SQL against my postgres db,
connection object I got through
import psycopg2
conn_string = "host='localhost' port='5432' dbname='postgres' user='postgres' password='mysecretpassword'"
conn = psycopg2.connect(conn_string)
seems to be OK
result = cursor.execute(
"""
select
*
from
planet_osm_point limit 10
""")
Result is Nonetype, so must be something wrong ?
What have I done wrong ? How could I debug this ?
cursor.execute() only executes the query, it does not fetch any data. In order to receive data, you will need to call cursor.fetchall() or cursor.fetchone().
import psycopg2
conn_string = "host = 'localhost' port = '5432' dbname = 'postgres' user = 'postgres' password = 'mysecretpassword'"
conn = psycopg2.connect(conn_string)
cursor.execute(
"""
select
*
from
planet_osm_point limit 10
""")
result = cursor.fetchall()
I have a mysql db. I set charset to utf8;
...
PRIMARY KEY (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
...
I connect to db in python with MySQLdb;
conn = MySQLdb.connect(host = "localhost",
passwd = "12345",
db = "db",
charset = 'utf8',
use_unicode=True)
When I execute a query, response is decoding with "windows-1254". Example response;
curr = conn.cursor(MySQLdb.cursors.DictCursor)
select_query = 'SELECT * FROM users'
curr.execute(select_query)
for ret in curr.fetchall():
username = ret["username"]
print "repr-username; ", repr(username)
print "username; "username.encode("utf-8")
...
output is;
repr-username; u'\xc5\u0178\xc3\xbckr\xc3\xbc\xc3\xa7a\xc4\u0178l\xc3\xbcli'
username; şükrüçağlüli
When I print username with "windows-1254" it works fine;
...
print "repr-username; ", repr(username)
print "username; ", username.encode("windows-1254")
...
Output is;
repl-username; u'\xc5\u0178\xc3\xbckr\xc3\xbc\xc3\xa7a\xc4\u0178l\xc3\xbcli'
username; şükrüçağlüli
When I try it with some other characters like cyrillic alphabet, decodeding is changed dinamicly. How can I prevent it?
I think the items where encoded wrong while INSERT to the database.
I recommend python-ftfy(from https://github.com/LuminosoInsight/python-ftfy) (helped me out in a simillar problem):
import ftfy
username = u'\xc5\u0178\xc3\xbckr\xc3\xbc\xc3\xa7a\xc4\u0178l\xc3\xbcli'
print ftfy.fix_text(username) # outputs şükrüçağlüli