【问题标题】:Connecting Xamarin to Python MySQL connector将 Xamarin 连接到 Python MySQL 连接器
【发布时间】:2018-08-17 16:34:27
【问题描述】:

我正在做一个 Xamarin 应用程序,我想使用 Python 连接器将它连接到 MySQL 数据库,但我不确定如何在 C# Xamarin 代码和 Python 连接器之间交换信息。例如,我想将用户在 Xamarin 应用程序中输入的登录信息发送到数据库。

Python 代码:

import mysql.connector
import requests
from datetime import date, datetime,timedelta

cnx = mysql.connector.connect(user='root',password='root',
                                database='user_account', host='localhost', port='3310')

cursor = cnx.cursor()
params = {'key1' : 'value1', 'key2' : 'value2', 'key3' : 'value3'}
r = self.request.GET.get('email')
print(r)

add_user = ("INSERT INTO login_info " "(email, password, date) " "VALUES (%s,%s,%s)")

user_data = (params[0], params[1], params[2])

cursor.execute(add_user, user_data)
cnx.commit()
cursor.close()

cnx.close()

C#代码:

public static void AddUserToDatabase()
        {
            WebClient client = new WebClient();
            Uri uri = new Uri("http://localhost/login.py");
            NameValueCollection postData = new NameValueCollection();
            postData.Add("email", "testEmail@email.com");
            postData.Add("password", "superTestPassword");
            postData.Add("date", DateTime.Now.ToShortDateString());
            Encoding.UTF8.GetString(client.UploadValues(uri, postData));
        }

感谢您的所有回答。

【问题讨论】:

    标签: c# python mysql database xamarin


    【解决方案1】:

    编辑:我误解了您使用 MySQL 的目的。

    好的,对于初学者来说,Internet 和 StackOverflow 上已有大量资源可以帮助回答这个问题。

    This is from a while back but should still work.

    不要在您的应用中与您的数据库建立直接连接。 相反,通过 Web API 或使用 Azure 等系统。有多种方法可以从应用程序中提取 C# 代码,坦率地说,它们非常常见,因此如果您的 Xamarin 代码中有登录凭据,请想象它们是公开的。

    【讨论】:

    • 用SQLite做注册数据库好不好(用户名、密码等),不是客户端吗?
    • 更新了我的答案。看看有没有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-25
    • 2023-03-12
    • 2021-03-21
    • 2021-03-31
    • 2018-06-27
    • 2016-01-16
    • 1970-01-01
    相关资源
    最近更新 更多