-1

I would like to create an application with a SQLite database but I do not know why I can not create the database

class Db_Connection(context: Context?) : SQLiteOpenHelper(context,"my_db",null, 1) {

    override fun onCreate(p0: SQLiteDatabase?) {

        var create_Table =
            "Create Table product( id INTEGER PRIMARY KEY AUTOINCREMENT ,  name TEXT)"
        p0!!.execSQL(create_Table)
    }

    override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) {
    }
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    Db_Connection(this)
}
6
  • What is the problem, exactly? Are you getting any errors or Exceptions? Commented Sep 10, 2019 at 8:39
  • i didn't get any thing Commented Sep 10, 2019 at 9:07
  • I'm not quite sure what you're asking, then. How are you determining that you "can not create the database"? Commented Sep 10, 2019 at 9:08
  • Are you trying to find the database file after running that code? Is that what you mean? Commented Sep 10, 2019 at 9:36
  • yes ,I did not find Device Folder Database in File Explorer> Data->Data->my BackageName Commented Sep 10, 2019 at 14:31

1 Answer 1

-1

Well this is java based code. Hope it can help you a bit

Create a database class and extends it to SQLiteOpenHelper class

public class DatabaseHelper extends SQLiteOpenHelper implements Constants {
private static final String dbname = "towers.db";
private static final int dbversion = 1;

public DatabaseHelper(Context context) {
    super(context, dbname, null, dbversion);
}

@Override
public void onCreate(SQLiteDatabase db) {
 //  String query = "CREATE TABLE TOWERS_TABLE (_id INTEGER PRIMARY KEY AUTOINICREMENT, CIRCLE TEXT, OPERATOR TEXT, NETWORK TEXT, CELLID TEXT, SIGNAL_STRENGTH TEXT)";
    String query =  "CREATE TABLE TOWERS (_id INTEGER PRIMARY KEY AUTOINICREMENT, DATA TEXT)";
    db.execSQL(query);

}

public void insertData(String data, SQLiteDatabase database){
    try {
        ContentValues values = new ContentValues();
        values.put("data",data);
        database.insert("TOWERS",null,values);
    }catch (SQLException e){
        e.printStackTrace();
    }

}

public void clearData(SQLiteDatabase db){
    String query = "DROP TABLE IF EXISTS TOWERS";
    db.execSQL(query);
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

Now create an object of database class where you want to add value

    databaseHelper = new DatabaseHelper(context);
        database = databaseHelper.getWritableDatabase();
 databaseHelper.insertData(data,database); //Here data is the value that you want to store
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.