Forum Archive

MySQL in pythonista

skaboy71

Hey there guys ... I'm a noob on both Python and pythonista. Is there a step by step I can follow that will allow me to connect to MySQL external data from my scripts in pythonista? I've installed pipista and stash but I can't seem to find or install a workable solution.

Thnx

skaboy71

I've created copys of all the files in the git-hub at https://github.com/tommasoturchi/mysql-connector-pythonista

I put them in a /site-packages/mysqldb folder but it looks like I need an init file to be able to import ?

looks like sometimes this is just a blank file but if that does not work … I have no idea where to go from there?

chriswilson

Hi @skaboy71

I use the same mysqldb module and it's great. Try putting the files in site-packages without them being in a subfolder. This works for me.

I use it in a Python 2.x environment, so not sure if it works in Python 3.x or not.

Be sure to import mysqldb at the start. This thread has some useful advice and code examples to get it going.

skaboy71

Thnx .. I'll give that a shot ! I'm using 2.7 so if that worked for you hopefully it will be the same for me.

chriswilson

If it's not working and you're using Pythonista 3, try putting the scripts in the site-packages-2 folder instead. It's for modules that don't work with Python 3.x (mysqldb might be compatible, but I'm not sure).

Here's a quick example using INSERT:

import mysqldb

db = mysqldb.connect(host= "mysql.my_domain.co.uk", user = "my_username", passwd="my_password", db="my_database", port = 3306)

c = db.cursor()

c.execute("INSERT INTO my_table (my_column1, my_column_2) VALUES (%s, %s)", (my_value_1, my_value_2))

db.commit()

db.disconnect()

And another using SELECT:

import mysqldb

db = mysqldb.connect(host= "mysql.my_domain.co.uk", user = "my_username", passwd="my_password", db="my_database", port = 3306)

c = db.cursor()

c.execute("SELECT * FROM my_table ORDER BY my_column_1 DESC LIMIT 0, 100")

my_variable = c.fetchall()

db.disconnect()
skaboy71

I got it working ! Thanks for your help chriswilson !

El_Cid

Is there an easier way to import the scripts from GitHub - or did you just create new files and copy - paste from safari or something?

Thanks!

abcabc

Try the following in stash

git clone https://github.com/tommasoturchi/mysql-connector-pythonista.git

El_Cid

thank you so much! I tried git pull before asking you! I need to learn to use github thats for sure :)

Thanks again!

El_Cid

It worked with the files moved to: \Modules & Templates\site-packages-3\
Thank you guys!

This app together with the service provided by PythonAnywhere are so amazing! They changed my life for the better :)

Edit: Actually with the files moved to site-packages-3 - I get an error in connection.py. So I think mysqldb doesn't support python 3.
So I moved the files to site-packages-2 - and I tried running the script with python 2.7 - but the script just hangs with these two lines:

import mysqldb

db = mysqldb.connect(host= "mysql.my_domain.co.uk", user = "my_username", passwd="my_password", db="my_database", port = 3306)

El_Cid

Actually it didn't just hang, it would eventually raise this error: ImportError: No localization support for language 'eng'.

It's probably a connection issue - but mysqldb doesn't raises the correct exception because he is looking for some file somewhere. Anyone have any clue?

ccc

No localization support for language 'eng'.

This might be related to https://github.com/omz/Pythonista-Issues/issues/27

El_Cid

I tried adding:

import locale

locale.setlocale(locale.LC_ALL, 'de_DE')

as they said - but I just get 'unsupported locale setting'

sethur

Thank you for the magnificent problem-solving information. Allow me to submit the following step-by-step instructions for installing MySQL access to pythonista for iOS; it worked for me.

https://mysql.wisborg.dk/2018/08/31/mysql-connector-python-on-ios-using-pythonista-3/