Reputation: 3481
Because I need to pre-populate the sqlite database, how can I operate on the database? Like create a table and insert some rows?
Upvotes: 1
Views: 294
Reputation: 32
From your question I think you are unfamiliar with the Database Handling In Android. You can very easily use a DatabaseHandler class and DatabaseHelper (extending SQLiteOpenHelper)
The code below explains it very well of how to use this class to do all the database operations
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHandler{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Fruitsdb";
private static final String TABLE_FRUITS = "fruits_table";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String CREATE_FRUITS_TABLE = "CREATE TABLE " + TABLE_FRUITS + " ("
+ KEY_ID + " INTEGER PRIMARY KEY autoincrement, " + KEY_NAME + " TEXT not null);";
private SQLiteDatabase db;
private final Context ct;
private DatabaseHelper dbhelper;
public DatabaseHandler(Context context){
this.ct=context;
dbhelper=new DatabaseHelper(ct,DATABASE_NAME,null,DATABASE_VERSION);
}
public DatabaseHandler open(){
db=dbhelper.getWritableDatabase();
return this;
}
public void close(){
db.close();
}
private static class DatabaseHelper extends SQLiteOpenHelper{
public DatabaseHelper (Context ct,String fruitname,CursorFactory cf,int ver){
super(ct,fruitname,cf,ver);
}
//this oncreate method is called only once when the database is created
//and this inserts the values into the tables the first time the app is started
@Override
public void onCreate(SQLiteDatabase sqldb) {
sqldb.execSQL(CREATE_FRUITS_TABLE);
List <String> lstfruits=new ArrayList<String>();
lstfruits.add("Orange");
lstfruits.add("Apple");
lstfruits.add("Guava");
lstfruits.add("Grapes");
for(String strFruits:lstfruits){
ContentValues cv=new ContentValues();
cv.put(KEY_NAME, strFruits);
sqldb.insert(TABLE_FRUITS, null, cv);
}
}
@Override
public void onUpgrade(SQLiteDatabase sqldb, int oldVersion, int newVersion) {
sqldb.execSQL("DROP TABLE OF EXISTS"+TABLE_FRUITS);
onCreate(sqldb);
}
}
public long insertEntry(String strFruit){
//write code here of insert and change the return statement
//using sqldb.insert(parameters..);
return -1;
}
public boolean removeEntry(String strFruit){
//write code here of delete a entery and change the return statement
//using sqldb.delete(parameters..);
return true;
}
}
You can call this class from your main Activity By
DatabaseHandler dbhandler=new DatabaseHandler(this);
dbhandler.open(); //The database is created and the values are inserted
//here you can do insert, updated ,remove,select operations
dbhandler.close(); // don't forget his one
This is the most simplest way of accessing and storing values in sqlite3 database. Hope it might solve your problem.
Upvotes: 0
Reputation: 145
You can use the SQLite3 tool available with the Android SDK. It is in the tools folder under SDK root.
Add the following directory to the PATH: {SDK root}\tools
Open a command window
Type sqlite3
Upvotes: 2
Reputation: 2452
many tools allow manipulating sqlite files. How about using the sqlite odbc driver and use a tool like microsoft access (if you have it)
http://www.ch-werner.de/sqliteodbc/
Upvotes: 0