Ehsan Akbar
Ehsan Akbar

Reputation: 7301

connect to sqlite in android using connection string

I am so new and new in android , i have a big problem with it, i create an app that needs to connect to database .so i create a database using sqllite expert pro as you can see here :

CREATE TABLE [user](
    [name] tEXT, 
    [id] INT PRIMARY KEY);

My database name is a .i want to read the value from the user table as you can see here in my code :

SQLiteDatabase mydatabase = openOrCreateDatabase("a",MODE_PRIVATE,null);
Cursor resultSet = mydatabase.rawQuery("Select * from user",null);
resultSet.moveToFirst();
String username = resultSet.getString(1);
String password = resultSet.getString(2);

  TextView tv = (TextView) findViewById(R.id.editText1);
    tv.setText("Text is: " + username + password);

but it doesn't work ,should i add connection string to my code ,or should i import the database into my project,because the database is in my workspace folder.

enter image description here

enter image description here

I use this code to create a test database inside android ,but it doesn't work again?

SQLiteDatabase mydatabase = openOrCreateDatabase("ali",MODE_PRIVATE,null);
        mydatabase.execSQL("CREATE TABLE IF NOT EXISTS TutorialsPoint(Username VARCHAR,Password VARCHAR);");
        mydatabase.execSQL("INSERT INTO TutorialsPoint VALUES('admin','admin');");

        Cursor resultSet = mydatabase.rawQuery("Select * from TutorialsPoint",null);
        resultSet.moveToFirst();
        String username = resultSet.getString(1);
        String password = resultSet.getString(2);

Upvotes: 0

Views: 3035

Answers (2)

akyirem samuel
akyirem samuel

Reputation: 78

Your database needs to be copied to the phone's internal storage first. You can do it manually or with the help of this library Android SqliteAsset Helper

Follow the right method for creating database in android using codes: Create a class that extends SqliteOpenHelper.

    public class DbOpener extends SqliteOpenHelper {
    DbOpener(Context c){
        super(c, 1, "a", null, 1); //where "a" is the database name
    }

    public void onCreate (SqliteDatabase db){
         db.execSQL("CREATE TABLE  TutorialsPoint (Username VARCHAR, Password VARCHAR);");
    }
}

Then in your activity use it as follows:

DbOpener opener = new DbOpener(this);

SqliteDatabase myDatabase = opener.getWritableDatabase();

//Now you can perform all your queries (including insertions) using the myDatabase object

Upvotes: 2

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29783

First, you should change your database name to a readable one like "mydata.db"

Second, there is no need for connection string on using SQLite in Android like the usual ways we do with accessing database from Java code.

You need to access database by using SQLiteOpenHelper. Tutorial from Android SQLite database and content provider. Try to get the feel with Android and SQLite from the tutorial.

Then, after you've mastering the concept and know the how, you can use your predefined database by utilizing Android SQLiteAssetHelper

Upvotes: 1

Related Questions