简体   繁体   English

如何将Android应用程序连接到MySql

[英]How to connect android application to MySql

I am making an application on Android that will record stats from a sports game, I want to store the information obtained from the app to a MySql database. 我正在Android上创建一个应用程序来记录体育游戏的统计数据,我想将从应用程序获得的信息存储到MySql数据库中。 Can someone point me in the right direction please 请有人指出我正确的方向

you just need to use any of network libraries like ( Volley , Retrofit , OkHttp ... etc ) if you use Volley Library you just need to install XAMPP and use phpMyAdmin to create your Database then learn mysql PHP using request methods like GET , POST then in Volley library in android just call ( Hashmap ) method to send data from android to mysql ... this picture will understand you what i meaning 你只需要使用任何网络库(如Volley,Retrofit,OkHttp等),如果你使用Volley Library你只需要安装XAMPP并使用phpMyAdmin创建你的数据库然后使用GET,POST等请求方法学习mysql PHP然后在android中的Volley库中调用(Hashmap)方法将数据从android发送到mysql ...这张图片会理解你的含义 排球机器人序列

On the app side you need network libraries that will send a request to the server to save/fetch the data from the server. 在应用程序端,您需要网络库,它将向服务器发送请求以从服务器保存/获取数据。 These can be Retrofit/Volley/OkHttp etc. These network libraries will use an API (URL) to send or retrieve the data from the server. 这些可以是Retrofit / Volley / OkHttp等。这些网络库将使用API​​(URL)从服务器发送或检索数据。

On the back end side, you will need a web service to connect the SQL database and the app. 在后端,您将需要一个Web服务来连接SQL数据库和应用程序。 The web service will provide an API (URL) that will be used by the App to send/retrieve the data. Web服务将提供应用程序用于发送/检索数据的API(URL)。 The web service can be made using PHP. Web服务可以使用PHP制作。

Of course you will need a server to host the web service files or locally you can try WAMP/XAMP etc. software to host and test the APIs but the mobile device and WAMP/XAMP server (ie computer/laptop) should be on the same network. 当然,您需要一台服务器来托管Web服务文件,或者您可以在本地尝试使用WAMP / XAMP等软件来托管和测试API,但移动设备和WAMP / XAMP服务器(即计算机/笔记本电脑)应该是相同的网络。

You will need to first initiate your sqlite database in its own class and then call it within your activity you want to store the results in eg 您需要首先在自己的类中启动sqlite数据库,然后在您要将结果存储在您的活动中调用它,例如

database class: 数据库类:

public class MyDB extends SQLiteOpenHelper {
//creates desired fields for app in db
public static final String DB_NAME = "mySQLiteDatabase";
public static final String TABLE_NAME = "results";
public static final String COLUMN_ID = "id";
//place your fields here e.g. result 1 

 public static final int DB_VERSION = 1;//version of database

public MyDB(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    String sql = "CREATE TABLE " + TABLE_NAME + " (" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " // add the fields 
created here 
    sqLiteDatabase.execSQL(sql);

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
    String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
    sqLiteDatabase.execSQL(sql);
    onCreate(sqLiteDatabase);

}  public boolean addResult(String result1, //etc 
   SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(//your column name, //your field );
    //other fields here e.g. result2 etc
       return sqLiteDatabase.insert(TABLE_NAME, null, contentValues) != -1;

}

after initialising something along the lines of this you can then use the addResult method in your main class 在初始化了一些内容之后,您可以在主类中使用addResult方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM