Home > Android > SQLite in android

SQLite in android


First we create a layout like this:

main.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<ScrollView xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >

<TextView
android:id=”@+id/lblText”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content” />

</ScrollView>

EventsData.java

package test.com.vn;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class EventsData extends SQLiteOpenHelper{

private static final String DATABASE_NAME=”events.db”;
private static final int DATABASE_VERSION =1;
public EventsData(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String sqlquery = “Create table events (_id integer primary key autoincrement,time integer, title text not null);”;
db.execSQL(sqlquery);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
String drop=”Drop table if exist events”;
db.execSQL(drop);
onCreate(db);
}

}

CanvasLessonActivity.java

package test.com.vn;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;

public class CanvasLessonActivity extends Activity {
/** Called when the activity is first created. */
private EventsData events;
private static String[] FROM ={“_id”,”time”,”title”};
private static String ORDER_BY=”time desc”;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
events = new EventsData(this);
try{
//addEvent(“Hello, Android!”);
addEvent(“Canvas Lesson”);
Cursor cursor = getEvents();
showEvents(cursor);
}finally{
events.close();
}

}

public void addEvent(String str){
SQLiteDatabase db = events.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(“time”, System.currentTimeMillis());
values.put(“title”, str);
db.insertOrThrow(“events”, null, values);
}

private Cursor getEvents(){
SQLiteDatabase db = events.getReadableDatabase();
Cursor cursor = db.query(“events”, FROM, null,null, null,null, ORDER_BY);
startManagingCursor(cursor);
return cursor;
}

private void showEvents(Cursor cursor){
StringBuilder builder = new StringBuilder(“Saved events: \n”);
while(cursor.moveToNext()){
long id = cursor.getLong(0);
long time = cursor.getLong(1);
String title = cursor.getString(2);
builder.append(id).append(“: “);
builder.append(time).append(“: “);
builder.append(title).append(“\n”);
TextView text = (TextView)findViewById(R.id.lblText);
text.setText(builder);
}
}
}

 

Categories: Android
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: