【问题标题】:I need to use SQLiteOpenHelper?我需要使用 SQLiteOpenHelper 吗?
【发布时间】:2015-07-01 20:42:17
【问题描述】:

我正在尝试将我的应用程序连接到我的电脑中的 Xampp mysql 服务器。问题是如果不知道我是否需要使用 SQLiteOpenHelper 是还是是?或者我可以在主要活动中做所有事情?这是我的代码:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
         if (conectarMySql()){
             Toast.makeText(getApplicationContext(), "WORKS", Toast.LENGTH_SHORT).show();
         }else{
             Toast.makeText(getApplicationContext(), "DONT WORK", Toast.LENGTH_SHORT).show();
         }

        }
    });


}


public boolean conectarMySql() {
    String xServidor = "10.0.2.2";
    String xPuerto = "3306";
    String xUsuario = "user1";
    String xPass = "pass1";
    String notwork = "not work";
    String xBase = "penia";
    String estado = "";
    boolean estadoConexion = false;
    Connection conexionMySql = null;

    String driver = "com.mysql.jdbc.Driver";
    String urlMySQL;
    urlMySQL = "jdbc:mysql://" + xServidor + ":" + xPuerto + "/";
    try {
        Class.forName(driver).newInstance();

        conexionMySql = DriverManager.getConnection(urlMySQL + xBase, xUsuario, xPass);

        if (!conexionMySql.isClosed()){
            Log.e(notwork,"CONEXION BIEN");
            estadoConexion = true;
            estado = "bien conexion";
        }

    }catch (Exception EX){
        Log.e(notwork,"Con");
    }


    Log.e(notwork,estado);
    return estadoConexion;

}
}

提前致谢:)

【问题讨论】:

    标签: java android mysql xampp


    【解决方案1】:

    在应用中创建 SQLite 数据库时使用 SQLiteOpenHelper。如果您想连接到应用程序外部的数据库,则不需要它。

    【讨论】:

    • 非常感谢,我不会忘记这个建议,一定要接受。但是代码仍然不起作用,它是否正确完成?也许我错过了什么?或者我需要 100% 使用 Web 服务在 XAMPP SQL 和我的 Android 应用程序之间进行通信?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多