Kivy SQLalchemy和Android,没有名为MySQLdb的模块

时间:2016-08-06 11:51:43

标签: android python mysql buildozer

我正在构建一个Android应用程序,我需要与Mysql数据库进行交互。我在配置Android的访问方面遇到了一些问题,但我的linux安装没有。

我在我的android apk上成功导入了SQLAlchemy的配方,但当我通过logcat运行应用程序时,我看到了错误:

NO module named MySQLdb

所以我尝试在buildozer中添加MySQLdb的配方,它说:

No matching distribution found for MySQLdb

这是调用mysql部分的代码的一部分:

import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy import MetaData, Column, Table, ForeignKey
from sqlalchemy import Integer, String
from sqlalchemy.sql import select

def check_sql_list(_id, string):
    cur = engine.connect()
    res = cur.execute("SELECT * FROM tbl_user")
    for x in res.fetchall():
        if x[_id] == string: return True
    cur.close()

def create_user(name,e_mail,psw):
    if check_sql_list(1,name):
        print 'Select another username.'
    elif check_sql_list(2,e_mail):
        print 'Your E-Mail have already an account.'
    else:
        connection = engine.raw_connection()
        try:
            cursor = connection.cursor()
            cursor.callproc("sp_createUser",[name, e_mail, psw])
            results = list(cursor.fetchall())
            for a in cursor.fetchall():
                print a
            print results
            cursor.close
            connection.commit()
        finally:
            connection.close()
            print 'Account registered succesfully.'

ip = 'localhost'
databs = 'mydb'
user = 'guest'
psw = 'password'
porta = '3306'

engine = create_engine('mysql://' + user + ':' + psw + '@' + ip + ':' + porta + '/' + databs)
create_user('user001','mail@001.com','password')

如何在不需要MySQLdb配方的情况下连接到我的数据库?或者我可以找到那个食谱?

2 个答案:

答案 0 :(得分:0)

我回答我自己的问题,希望有人会发现它有用。

我找到了如何解决它,用作拨号器mysql + mysqlconnector,如:

engine = create_engine('mysql+mysqlconnector://user00:psw@localhost:3306/database')

并在buildozer.spec中添加配方要求:

requirements = kivy,sqlalchemy,mysql_connector

答案 1 :(得分:0)

  

kivy -m pip install mysql-connector

比:

  

导入mysql.connector

为我工作:)