// Sqlite database using SQLiteOpenHelper
Creating a table named as company_table.
Here company_table is table name,
company.db is database name.
package com.****;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class StoringCompany extends SQLiteOpenHelper
{
private static final String DATABASE_NAME = "company.db";
private static final int DATABASE_VERSION = 1;
//object for SQlite database
SQLiteDatabase dbase;
SQLiteDatabase dbase;
public StoringCompany(Context context, String name, CursorFactory factory,int version)
{
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
try
{
Log.i("table creating","company table creating");
String CREATE_COMPANY_TABLE = "CREATE TABLE company_table(id INTEGER,company_name TEXT);";
db.execSQL(CREATE_COMPANY_TABLE);
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS company_table");
// Create tables again
onCreate(db);
}
}
The table values are stored from an another one class.. that is..
// TODO Auto-generated method stub
SQLiteDatabase dbase;
dbase=helpercompany.getReadableDatabase();
dbase=helpercompany.getWritableDatabase();
ContentValues value=new ContentValues();
value.put("id",id);
value.put("company_name",company_name);
dbase.insert(companytable, null, value);
}
//Object created for class and values passed to a String.
StoringCompany helpercompany;
helpercompany=new StoringCompany(this, null, null, 0);
//table name stored in a String
String companytable="company_table";
private void updating_company(String id, String company_name)
{// TODO Auto-generated method stub
SQLiteDatabase dbase;
dbase=helpercompany.getReadableDatabase();
dbase=helpercompany.getWritableDatabase();
ContentValues value=new ContentValues();
value.put("id",id);
value.put("company_name",company_name);
dbase.insert(companytable, null, value);
}
http://sourcecodeandroid.blogspot.in by T.s.Mathavan
Comments