Archive for the ‘MySQL’ Category

Mysql from an external file

Sunday, July 26th, 2009

From the command-line:

mysql -u username -ppassword database < filename

From within MySQL:

Log into mysql i.e. mysql -u -p [database]
mysql> source [filename]

Python MySQL utf connection + dictionary recordsets

Tuesday, July 14th, 2009

# -*- coding: utf-8 -*-
import MySQLdb, MySQLdb.cursors
db_connection = MySQLdb.connect(user="root", passwd="pass",
db="mydb", cursorclass = MySQLdb.cursors.DictCursor,
use_unicode = True)
db = db_connection.cursor()
# assuming that id=1271 has a val that has utf "ยต" in it
sql = """
SELECT *
FROM vals
WHERE id = 1271
""" % ()
db.execute(sql)
r = db.fetchall()[0]
val = r['val']
print val
print type(val)