SiDzej
SiDzej

Reputation: 72

Connecting Xamarin to Python MySQL connector

I'm doing an Xamarin app, and I want to connect it to MySQL databse using Python connector, and I'm not sure how could I exchange information between C# Xamarin code and Python Connector. For example I'd want to send login that user has input in the Xamarin app to the database.

Python code:

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# code:

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

Thanks for all your answers.

Upvotes: 0

Views: 195

Answers (1)

jamesfdearborn
jamesfdearborn

Reputation: 799

EDIT: I misunderstood the purpose you were using MySQL for.

Ok, for starters there are a great number of preexisting resources on the Internet and StackOverflow to help answer this very question.

This is from a while back but should still work.

Do not establish a direct connection to your database in your app. Instead, go through web APIs or use systems like Azure. There are ways to extract the C# code from an application and they're frankly very common so if you have login credentials in your Xamarin code imagine them public.

Upvotes: 1

Related Questions