简体   繁体   English

连接到localhost数据库

[英]Connect to localhost database

I got a little question about databases and android. 我有一个关于数据库和android的问题。 I got this code: 我得到了这段代码:

sampleDB =  this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);

            sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                    SAMPLE_TABLE_NAME +
                    " (LastName VARCHAR, FirstName VARCHAR," +
                    " Country VARCHAR, Age INT(3));");

            sampleDB.execSQL("INSERT INTO " +
                    SAMPLE_TABLE_NAME +
                    " Values ('Makam','Sai Geetha','India',25);");

and to read: 并阅读:

if (c != null ) {
    if  (c.moveToFirst()) {
        do {
            String firstName = c.getString(c.getColumnIndex("FirstName"));
            int age = c.getInt(c.getColumnIndex("Age"));
            results.add("" + firstName + ",Age: " + age);
        }while (c.moveToNext());
    } 
}

With this code, I make and read the database, and insert some info in it. 使用此代码,我创建并读取数据库,并在其中插入一些信息。 And print it on the screen, this all works :) 并将其打印在屏幕上,这一切都有效:)

Now the part I can't figure out: 现在这部分我无法弄清楚:
I use myPHPadmin (with xampp) , 我使用myPHPadmin (with xampp)

I made the exact database as I do in the code. 我像在代码中一样制作了确切的数据库。

But how do I connect, so my code reads that database. 但我如何连接,所以我的代码读取该数据库。

It is a local database for now (127.0.0.1) . 它现在是一个本地数据库(127.0.0.1)

Is it possible to connect a local database? 是否可以连接本地数据库? (if so, could you tell me how to) (如果是的话,你能告诉我怎么做)

Do you need PHP, or can you do everything in (Android) Java code? 你需要PHP,还是你可以用(Android)Java代码做所有事情?

I am totally new with databases, so sometimes it confusing for me. 我对数据库很新,所以有时候我会感到困惑。

Please put me in the good direction. 请把我放在好的方向。

If you need more information for the question or something else, please let me know. 如果您需要有关该问题或其他内容的更多信息,请告诉我们。

It is a local database for now ( 127.0.0.1 ). 它现在是一个本地数据库( 127.0.0.1 )。

In Android you have to use 10.0.2.2 or System's Static IP . 在Android中,您必须使用10.0.2.2系统的静态IP

Write a PHP script (You can also use other but PHP its easy to implement) to manage the database and run this script using HTTP protocol from the android system. 编写一个PHP脚本(您也可以使用其他但易于实现的PHP)来管理数据库并使用Android系统中的HTTP协议运行此脚本。

These Tutorials might help you: 这些教程可能会帮助您:

As far as I'm aware there is no MySQL library for android. 据我所知,Android没有MySQL库。 But you can use the HttpPost to send data to a server side script (such as PHP) and then return it in a format you can parse in your Android application. 但是您可以使用HttpPost将数据发送到服务器端脚本(例如PHP),然后以您可以在Android应用程序中解析的格式返回它。

There's a nice tutorial on how to achieve this here: http://www.helloandroid.com/tutorials/connecting-mysql-database 这里有一个很好的教程如何实现这个目的: http//www.helloandroid.com/tutorials/connecting-mysql-database

Here's a link to the HttpPost Documentation: http://developer.android.com/reference/org/apache/http/client/methods/HttpPost.html 这是HttpPost文档的链接: http//developer.android.com/reference/org/apache/http/client/methods/HttpPost.html

Hope this helps, this is a good way to get you started communicating with external MySQL databases within an Android application. 希望这会有所帮助,这是让您开始与Android应用程序中的外部MySQL数据库进行通信的好方法。

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

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