0

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);
4
  • When you crete and Database in Android, it will not be in your workspace or project file, it will be on the Device that is running the application, so you need to create the table in your code, and full fill the data. I recommend read about DatabaseHelper, create a class to handle the Database. Or use some library like Ormlite Commented Jun 30, 2016 at 13:05
  • So you mean i should create my database using code ,is there any wizard tools that i can use it to create my database graphically ?like sqlesxpress in vs? Commented Jun 30, 2016 at 13:10
  • @EhsanAkbar refer this its very well explained Commented Jun 30, 2016 at 13:13
  • 1
    @EhsanAkbar no, there is not graphical toll for create on Android, because the database will be created on each device but, exists libraries who help a lot, try read about Ormlite. BTW consider use the Android Studio, he have a better support from Google Commented Jun 30, 2016 at 14:14

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

thank you ,i add some details to my question ,you said i should create the database inside my code ,i did that but it didn't work
1

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

3 Comments

thank you ,i add some details to my question ,you said i should create the database inside my code ,i did that but it didn't work
Your code won't work and it's expected. What I meant is, you mingling your knowledge about database in Java for android. You need to use SQLiteOpenHelper to create and access SQLite database.
Please read and follow Android SQLite database tutorial first. Mastering the concept first and try to create a simple project based on it. After that, you can learn using your predefined database with SQLiteAssetHelper.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.