http://android.bigresource.com/Android-Want-Database-to-save-retrieve-names-in-phone-application-haK6YhJIr.html
main.xml
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:text="@string/hello" />
android:layout_height="wrap_content"
android:text="" />
DataHelper.java
package com.ram.sampleDB;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
public class DataHelper {
private static final String DATABASE_NAME = "example.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "table1";
private Context context;
private SQLiteDatabase db;
private SQLiteStatement insertStmt;
private static final String INSERT = "insert into "
+ TABLE_NAME + "(name) values (?)";
public DataHelper(sampleDB sampleDB) {
// TODO Auto-generated constructor stub
this.context = sampleDB;
OpenHelper openHelper = new OpenHelper(this.context);
this.db = openHelper.getWritableDatabase();
this.insertStmt = this.db.compileStatement(INSERT);
}
public void deleteAll() {
// TODO Auto-generated method stub
this.db.delete(TABLE_NAME, null, null);
}
public long insert(String name) {
// TODO Auto-generated method stub
this.insertStmt.bindString(1, name);
return this.insertStmt.executeInsert();
}
public List
// TODO Auto-generated method stub
List
Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" },
null, null, null, null, "name desc");
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return list;
}
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT)");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("Example", "Upgrading database, this will drop tables and recreate.");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}
sampleDB.java
package com.ram.sampleDB;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class sampleDB extends Activity {
/** Called when the activity is first created. */
private TextView output;
private DataHelper dh;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.output = (TextView) this.findViewById(R.id.out_text);
this.dh = new DataHelper(this);
this.dh.deleteAll();
this.dh.insert("Porky Pig");
this.dh.insert("Foghorn Leghorn");
this.dh.insert("Yosemite Sam");
List
StringBuilder sb = new StringBuilder();
sb.append("Names in database:\n");
for (String name : names) {
sb.append(name + "\n");
}
Log.d("EXAMPLE", "names size - " + names.size());
this.output.setText(sb.toString());
}
}
No comments:
Post a Comment