skip to Main Content

I am new to this technology. But currently I am working on a project on Android which has an integrated module where I use Python code because I need to implement ML to fetch some data from MySQL database.

Before applying ML I am simply trying to fetch data using Python script in my Android Application integrated with my Project.

For MySQL I have a XAMPP Server on my PC Installed.

Whenever I try to execute that application it always Crashes with the error.

2023-05-02 12:57:57.277 23850-23850/com.example.chaquopy E/USNET: USNET: appName: com.example.chaquopy
2023-05-02 12:57:59.426 23850-23850/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.chaquopy, PID: 23850
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chaquopy/com.example.chaquopy.MainActivity}: com.chaquo.python.PyException: InterfaceError: 2003: Can't connect to MySQL server on '192.168.137.1:3306' (1 Operation not permitted)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4035)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4201)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2438)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:226)
        at android.os.Looper.loop(Looper.java:313)
        at android.app.ActivityThread.main(ActivityThread.java:8663)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
     Caused by: com.chaquo.python.PyException: InterfaceError: 2003: Can't connect to MySQL server on '192.168.137.1:3306' (1 Operation not permitted)
        at <python>.mysql.connector.network.open_connection(network.py:602)
        at <python>.mysql.connector.connection._open_connection(connection.py:570)
        at <python>.mysql.connector.abstracts.connect(abstracts.py:1181)
        at <python>.mysql.connector.connection.__init__(connection.py:164)
        at <python>.mysql.connector.pooling.connect(pooling.py:294)
        at <python>.myScript.<module>(myScript.py:13)
        at <python>.importlib._bootstrap._call_with_frames_removed(<frozen importlib._bootstrap>:219)
        at <python>.importlib._bootstrap_external.exec_module(<frozen importlib._bootstrap_external>:843)
        at <python>.java.android.importer.exec_module(importer.py:554)
        at <python>.importlib._bootstrap._load_unlocked(<frozen importlib._bootstrap>:671)
        at <python>.importlib._bootstrap._find_and_load_unlocked(<frozen importlib._bootstrap>:975)
        at <python>.importlib._bootstrap._find_and_load(<frozen importlib._bootstrap>:991)
        at <python>.importlib._bootstrap._gcd_import(<frozen importlib._bootstrap>:1014)
        at <python>.importlib.import_module(__init__.py:127)
        at <python>.chaquopy_java.Java_com_chaquo_python_Python_getModuleNative(chaquopy_java.pyx:129)
        at com.chaquo.python.Python.getModuleNative(Native Method)
        at com.chaquo.python.Python.getModule(Python.java:84)
        at com.example.chaquopy.MainActivity.onCreate(MainActivity.java:31)
        at android.app.Activity.performCreate(Activity.java:8290)
        at android.app.Activity.performCreate(Activity.java:8270)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4009)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4201) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2438) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loopOnce(Looper.java:226) 
        at android.os.Looper.loop(Looper.java:313) 
        at android.app.ActivityThread.main(ActivityThread.java:8663) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135) 

Here’s my Code:

MainActivity.java
package com.example.chaquopy;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;

public class MainActivity extends AppCompatActivity {

        TextView textview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textview=findViewById(R.id.tv1);

        if (! Python.isStarted()) {
            Python.start(new AndroidPlatform(this));
        }//will start python

        //create python instance
        Python py=Python.getInstance();

        //create python object
        PyObject pyobj=py.getModule("myScript");//giving python script name

        //return text in TextView
        textview.setText(py.toString());
    }
}
myScript.py
import mysql.connector as mysql

# enter your server IP address/domain name
HOST = "192.168.137.1" # or "domain.com"

# database name, if you want just to connect to MySQL server, leave it empty
DATABASE = "recipe"
# this is the user you create
USER = "test"
# user password
PASSWORD = "123456"
# connect to MySQL server
connection = mysql.connect(host=HOST, port="3306", database=DATABASE, user=USER, password=PASSWORD)

cursor = connection.cursor()
# some other statements  with the help of cursor
retrive = "Select ingredients from final_merge;"

#executing the quires
cursor.execute(retrive)
rows = cursor.fetchall()
for row in rows:
    print(row)


#commiting the connection then closing it.
connection.commit()
connection.close()

Here 192.168.137.1 is Gateway IP of the Mobile Phone and that mobile is connected to the HotSpot on my Laptop.

The same Python Script works fine when I try to execute it on my laptop in PyCharm but when I try to run it on my Mobile Phone it Crashes.

I need a way where I should be able to connect with the database on my PC and run this code.

2

Answers


  1. Are python versions same on phone and laptop? Does phone have a firewall running? What TCPIP address is shown on your phone and can you phone ping the mySQL server IP? Have you tried running a connection from your phone via VPN to the mySQL box to see if that gets around the problem?

    https://android.stackexchange.com/questions/46499/how-configure-the-dhcp-settings-of-wifi-tethering-hotspot-on-android

    Also you could look at installing something like Termux

    Login or Signup to reply.
  2. Your app may be missing the INTERNET permission. See this question for details.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search