import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class DbaAdapter extends SQLiteOpenHelper {
public DbaAdapter(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
System.out.println("..........!!!!!!!!!..........");
db.execSQL("create table friends(lastname text,firstname text,country text,age integer);");
System.out.println("table created........!!!!!!!");
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
Database .java
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class DyDatabase extends Activity {
/** Called when the activity is first created. */
Button b1,b2,b3,b4;
TextView tv;
SQLiteDatabase db;
DbaAdapter helper;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
helper=new DbaAdapter(this,"MYDB.db",null,1);
db=helper.getWritableDatabase();
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
b4=(Button)findViewById(R.id.button4);
tv=(TextView)findViewById(R.id.tv1);
b1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
for(int i=0;i<2;i++)
{
ContentValues cv=new ContentValues();
cv.put("lastname","giroti");
cv.put("firstname", "dheeraj");
cv.put("country","india");
cv.put("age", 22);
db.insertOrThrow("friends", null, cv);
ContentValues cv1=new ContentValues();
cv1.put("lastname","raut");
cv1.put("firstname", "nupur");
cv.put("country","india");
cv1.put("age", 20);
db.insertOrThrow("friends", null, cv1);
}
tv.setText("value inserted");
}
});
b3.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//db.delete(table, whereClause, whereArgs)
ContentValues cv=new ContentValues();
cv.put("age", 23);
int k=db.update("friends",cv,"age=22", null);
tv.setText("tabel updated"+k);
}
});
b2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
db.execSQL("drop table friends");
tv.setText("tabel deleted");
}
});
b4.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String res="";
Cursor c=db.query("friends",null,null,null,null,null,null);
if(c.moveToFirst())
{
do
{
res+=c.getString(c.getColumnIndex("lastname"))+c.getString(c.getColumnIndex("firstname"))+c.getString(c.getColumnIndex("country"));
}while(c.moveToNext());
}
c.close();
tv.setText(res);
}
});
}
}
No comments:
Post a Comment