android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_create"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="10dp"
android:text="Create" />
<Button
android:id="@+id/btn_update_db"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="10dp"
android:text="UpdateDatabase" />
<Button
android:id="@+id/btn_add"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="10dp"
android:text="Add" />
<Button
android:id="@+id/btn_delete"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="10dp"
android:text="delete" />
<Button
android:id="@+id/btn_update"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="10dp"
android:text="Update" />
<Button
android:id="@+id/btn_query"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="10dp"
android:text="Query" />
</LinearLayout>
package com.example.mysqldemo;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper
{
private static final int VERSION = 1;
public static final String table_name="uer";
/**
* 带全部参数的构造函数 ,必不可少
*
* @param context
* @param name
* @param factory
* @param version
*/
public DatabaseHelper(Context context, String name, CursorFactory factory, int version)
{
super(context, name, factory, version);
}
// 带两个参数的构造函数,调用的其实是带三个参数的构造函数
public DatabaseHelper(Context context, String dbname)
{
this(context, dbname, VERSION);
}
// 带三个参数的构造函数,调用的其实是全部参数的构造函数
public DatabaseHelper(Context context, String name, int version)
{
this(context, name, null, version);
}
@Override
public void onCreate(SQLiteDatabase db)
{
// String sql = "create table user(id int,name varchar(20))";
String sql = "create table "+table_name+"(id int,name varchar(20))";
db.execSQL(sql);
System.out.println("DatabaseHelper Create()");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
System.out.println("Update" + "oldVersion=" + oldVersion + "newVewsion" + newVersion);
}
}
package com.example.mysqldemo;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener
{
private Button btn_create;
private Button btn_update_db;
private Button btn_add;
private Button btn_delete;
private Button btn_update;
private Button btn_query;
private String db_name = "test_db";
private int db_version = 1;
private DatabaseHelper mDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_add = (Button) findViewById(R.id.btn_add);
btn_create = (Button) findViewById(R.id.btn_create);
btn_delete = (Button) findViewById(R.id.btn_delete);
btn_query = (Button) findViewById(R.id.btn_query);
btn_update = (Button) findViewById(R.id.btn_update);
btn_update_db = (Button) findViewById(R.id.btn_update_db);
btn_add.setOnClickListener(this);
btn_create.setOnClickListener(this);
btn_delete.setOnClickListener(this);
btn_query.setOnClickListener(MainActivity.this);
btn_update.setOnClickListener(this);
btn_update_db.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v)
{
int id = v.getId();
switch (id)
{
case R.id.btn_create:
{
mDatabaseHelper = new DatabaseHelper(getApplicationContext(), db_name, db_version);
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
System.out.println("btn_create");
}
break;
case R.id.btn_update_db:
{
mDatabaseHelper = new DatabaseHelper(getApplicationContext(), db_name, db_version + 1);
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
System.out.println("btn_update_db");
}
break;
case R.id.btn_add:
{
mDatabaseHelper = new DatabaseHelper(getApplicationContext(), db_name);
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
ContentValues value = new ContentValues();
for (int i = 0; i < 10; i++)
{
value.put("id", "" + i);
value.put("name", "zhangsan" + i);
db.insert(DatabaseHelper.table_name, null, value);
System.out.println("add");
}
}
break;
case R.id.btn_delete:
{
mDatabaseHelper = new DatabaseHelper(getApplicationContext(), db_name);
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
db.delete(DatabaseHelper.table_name, "id=?", new String[] { "2" });
System.out.println("delete");
}
break;
case R.id.btn_update:
{
mDatabaseHelper = new DatabaseHelper(getApplicationContext(), db_name);
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "lisi");
db.update(DatabaseHelper.table_name, values, "id=?", new String[] { "1" });
System.out.println("update");
}
break;
case R.id.btn_query:
{
mDatabaseHelper = new DatabaseHelper(getApplicationContext(), db_name);
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
Cursor cursor = db.query(DatabaseHelper.table_name, new String[] { "id", "name" }, "id=?", new String[] { "1" }, null, null, null);
while (cursor.moveToNext())
{
String name = cursor.getString(cursor.getColumnIndex("name"));
System.out.println("name=" + name);
}
}
System.out.println("query");
break;
default:
break;
}
}
}