简体   繁体   中英

I need to use SQLiteOpenHelper?

Im trying to connect my application to a Xampp mysql server that i have in my pc. The question is if dont know if i need to use SQLiteOpenHelper yes or yes? Or i can do all in the main activity? Here goes my code:

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;

}
}

Thanks in advance :)

You use SQLiteOpenHelper only when you create a SQLite database in the app. If you want to connect to database outside the app then you don't need it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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