package
com.example.sqlite_01;
import
android.support.v7.app.ActionBarActivity;
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
ActionBarActivity {
private
Button createButton =
null
;
private
Button updateButton =
null
;
private
Button insertButton =
null
;
private
Button updateRecordButton =
null
;
private
Button queryRecordButton =
null
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createButton = (Button) findViewById(R.id.createButton);
updateButton = (Button) findViewById(R.id.updateButton);
insertButton = (Button) findViewById(R.id.insertButton);
updateRecordButton = (Button) findViewById(R.id.updateRecordButton);
queryRecordButton = (Button) findViewById(R.id.queryRecordButton);
createButton.setOnClickListener(
new
CreateListener());
updateButton.setOnClickListener(
new
UpdateListener());
insertButton.setOnClickListener(
new
InsertListener());
updateRecordButton.setOnClickListener(
new
UpdateRecordListener());
queryRecordButton.setOnClickListener(
new
QueryListener());
}
class
CreateListener
implements
OnClickListener{
@Override
public
void
onClick(View v) {
DatabaseHelper dbHelper =
new
DatabaseHelper(MainActivity.
this
,
"test_db"
);
SQLiteDatabase db = dbHelper.getReadableDatabase();
//执行了这一句才会创建数据库
}
}
class
UpdateListener
implements
OnClickListener{
@Override
public
void
onClick(View v) {
DatabaseHelper dbHelper =
new
DatabaseHelper(MainActivity.
this
,
"test_db"
,
2
);
SQLiteDatabase db = dbHelper.getReadableDatabase();
//执行了这一句才会创建数据库
}
}
class
InsertListener
implements
OnClickListener{
@Override
public
void
onClick(View v) {
ContentValues contentValues =
new
ContentValues();
contentValues.put(
"id"
,
1
);
contentValues.put(
"name"
,
"umgsai"
);
DatabaseHelper dbHelper =
new
DatabaseHelper(MainActivity.
this
,
"test_db"
,
2
);
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.insert(
"user"
,
null
, contentValues);
}
}
class
UpdateRecordListener
implements
OnClickListener{
@Override
public
void
onClick(View v) {
DatabaseHelper dbHelper =
new
DatabaseHelper(MainActivity.
this
,
"test_db"
,
2
);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values =
new
ContentValues();
values.put(
"name"
,
"admin"
);
db.update(
"user"
, values,
"id = ?"
,
new
String[]{
"1"
});
}
}
class
QueryListener
implements
OnClickListener{
@Override
public
void
onClick(View v) {
DatabaseHelper dbHelper =
new
DatabaseHelper(MainActivity.
this
,
"test_db"
,
2
);
SQLiteDatabase db = dbHelper.getReadableDatabase();
//db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy)
Cursor cursor = db.query(
"user"
,
new
String[]{
"id"
,
"name"
},
"id = ?"
,
new
String[]{
"1"
},
null
,
null
,
"id"
);
while
(cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(
"name"
));
System.out.println(
"name>>>"
+ name);
}
}
}
}