|
package tpg.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
public class DatabaseHandler extends SQLiteOpenHelper implements Runnable
{
protected SQLiteDatabase mbDataBase;
protected final Context mbContext;
protected String db_name;
protected SQLiteStatement insertStmt;
protected static String DB_PATH = "/data/data/linkwithweb/databases/";
protected static final String table_name="SampleCursorLoaderData";
private static String TAG="DatabaseHandler";
public static class UserTable {
public static String id="_id";
public static String name="name";
}
protected DatabaseHandler(Context context, String name, CursorFactory factory,int version){
super(context, name, factory, version);
this.mbContext = context;
this.db_name = name;
Thread t=new Thread(this);
t.start();
try{
if(t.isAlive()){
t.join();
}
}catch(Exception e){
e.printStackTrace();
}
}
public void onCreate(SQLiteDatabase db){
Log.v(TAG,"OnCreate method SQliteHelper");
String str="create table "+table_name+" ( "+UserTable.id+" TEXT, "+ UserTable.name+" TEXT);";
db.execSQL(str);
}
public void run(){
mbDataBase = getWritableDatabase();
Log.v(TAG,"Database object is created ="+mbDataBase);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
mbDataBase.execSQL("DROP TABLE IF EXISTS " + table_name);
onCreate(mbDataBase);
}
// public synchronized void close()
// {
// if (mbDataBase != null)
// {
// mbDataBase.close();
// }
// super.close();
// }
protected SQLiteDatabase getDb()
{
return mbDataBase;
}
protected void insert()
{
Log.v(TAG,"Inserting data in db");
ContentValues cv=new ContentValues();
cv.put(UserTable.id, 1);
cv.put(UserTable.name, "Inserting Row 1");
mbDataBase.insert(table_name,null, cv);
cv=null;
cv=new ContentValues();
cv.put(UserTable.id, 2);
cv.put(UserTable.name, "Inserting Row 2");
mbDataBase.insert(table_name,null, cv);
cv=new ContentValues();
cv.put(UserTable.id, 3);
cv.put(UserTable.name, "Inserting Row 3");
mbDataBase.insert(table_name,null, cv);
cv=new ContentValues();
cv.put(UserTable.id, 4);
cv.put(UserTable.name, "Inserting Row 4");
mbDataBase.insert(table_name,null, cv);
}
public Cursor queryData(){
Cursor cursor = this.mbDataBase.query(table_name, null,null, null, null, null, null);
/*if (cursor != null && !cursor.isClosed()) {
cursor.close();
}*/
return cursor;
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.