Skip to content

Instantly share code, notes, and snippets.

@SZooo
Last active August 29, 2015 14:05
Show Gist options
  • Save SZooo/112a0140930158d60ebd to your computer and use it in GitHub Desktop.
Save SZooo/112a0140930158d60ebd to your computer and use it in GitHub Desktop.
Test the database operation
public class openHelper extends SQLiteOpenHelper {
private static final int VERSION = 1;
public openHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
public openHelper(Context context,String name){
this(context,name,VERSION);
}
public openHelper(Context context,String name,int version){
this(context, name,null,version);
}
@Override
public void onCreate(SQLiteDatabase db) {
System.out.println("datehleper onCreate");
db.execSQL("create table HenHao(id int,name varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
System.out.println("-------updata database-----");
}
}
public class sqlActivity extends Activity {
private Button createbtn;
private Button updatebbtn;
private Button insertbtn;
private Button updatebtn;
private Button querybtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlbtn);
createbtn = (Button) findViewById(R.id.createbtn);
updatebbtn = (Button) findViewById(R.id.updatebbtn);
insertbtn = (Button) findViewById(R.id.insertbtn);
updatebtn = (Button) findViewById(R.id.updatebtn);
querybtn = (Button) findViewById(R.id.querybtn);
createbtn.setOnClickListener(new createListener());
updatebbtn.setOnClickListener(new updatebListener());
insertbtn.setOnClickListener(new insertListener());
updatebtn.setOnClickListener(new updataRR());
querybtn.setOnClickListener(new queryListener());
}
class createListener implements View.OnClickListener {
@Override
public void onClick(View v) {
openHelper dbh = new openHelper(sqlActivity.this, "Viho_db");
SQLiteDatabase db = dbh.getReadableDatabase();
}
}
class updatebListener implements View.OnClickListener {
@Override
public void onClick(View v) {
openHelper dbh = new openHelper(sqlActivity.this, "Viho_db", 2);
SQLiteDatabase db = dbh.getReadableDatabase();
}
}
class insertListener implements View.OnClickListener {
@Override
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put("id", 1);
values.put("name", "vihoTi");
openHelper dbh = new openHelper(sqlActivity.this, "Viho_db", 2);
SQLiteDatabase db = dbh.getWritableDatabase();
db.insert("HenHao", null, values);
}
}
class updataRR implements View.OnClickListener {
@Override
public void onClick(View v) {
openHelper dbh = new openHelper(sqlActivity.this, "Viho_db", 2);
SQLiteDatabase db = dbh.getReadableDatabase();
ContentValues values = new ContentValues();
values.put("name", "DavilKe");
db.update("HenHao", values, "id=?", new String[]{"1"});
}
}
class queryListener implements View.OnClickListener {
@Override
public void onClick(View v) {
System.out.println("aaa___________---------------------------->");
Log.d("this is my first sql", "do you like it");
openHelper dbh = new openHelper(sqlActivity.this, "Viho_db", 2);
SQLiteDatabase db = dbh.getReadableDatabase();
Cursor cursor = db.query("HenHao", new String[]{"id", "name"}, "id=?", new String[]{"1"}, null, null, null);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
System.out.println("query------->" + name);
}
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world!歡迎使用數據庫"
android:textSize="40dip" />
<Button
android:id="@+id/createbtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Create Database" />
<Button
android:id="@+id/updatebbtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Update Database" />
<Button
android:id="@+id/insertbtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Insert" />
<Button
android:id="@+id/updatebtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Update" />
<Button
android:id="@+id/querybtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="query" />
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment