package com.example.day_02_sqlite;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class MainActivity extends Activity {
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = openOrCreateDatabase("db", MODE_PRIVATE, null);
// db.insert(table, nullColumnHack, values)
// db.delete(table, whereClause, whereArgs)
// db.update(table, values, whereClause, whereArgs)
// db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit)
db.execSQL("create table student(id integer primary key autoincrement,name varchar(20),sex varchar(20))");
}
// 插入
public void insert(View v) {
db.execSQL("insert into student (name,sex) values (?,?)",new String[]{"张三","男"});
db.execSQL("insert into student (name,sex) values (?,?)",new String[]{"李四","女"});
db.execSQL("insert into student (name,sex) values ('王五','妖')"/*,new String[]{"王五","妖"}*/);
}
// 删
public void delete(View v) {
db.execSQL("delete from student where name = ?",new String[]{"王五"});
}
// 改
public void update(View v) {
db.execSQL("update student set name = ? where name = ?",new String[]{"张二","张三"});
}
// 查
public void select(View v) {
Cursor cursor = db.rawQuery("select * from student where sex = ?",new String[]{"妖"});
// cursor.
//是否可以向下移动,如果可以移动就代表有数据
while (cursor.moveToNext()) {
//先得到name列的角标,再通过角标获取name列的数据
String name =cursor.getString(cursor.getColumnIndex("name"));
String sex =cursor.getString(cursor.getColumnIndex("sex"));
Log.i("MainActivity", "name =" + name +" sex="+sex);
}
}
}
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class MainActivity extends Activity {
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = openOrCreateDatabase("db", MODE_PRIVATE, null);
// db.insert(table, nullColumnHack, values)
// db.delete(table, whereClause, whereArgs)
// db.update(table, values, whereClause, whereArgs)
// db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit)
db.execSQL("create table student(id integer primary key autoincrement,name varchar(20),sex varchar(20))");
}
// 插入
public void insert(View v) {
db.execSQL("insert into student (name,sex) values (?,?)",new String[]{"张三","男"});
db.execSQL("insert into student (name,sex) values (?,?)",new String[]{"李四","女"});
db.execSQL("insert into student (name,sex) values ('王五','妖')"/*,new String[]{"王五","妖"}*/);
}
// 删
public void delete(View v) {
db.execSQL("delete from student where name = ?",new String[]{"王五"});
}
// 改
public void update(View v) {
db.execSQL("update student set name = ? where name = ?",new String[]{"张二","张三"});
}
// 查
public void select(View v) {
Cursor cursor = db.rawQuery("select * from student where sex = ?",new String[]{"妖"});
// cursor.
//是否可以向下移动,如果可以移动就代表有数据
while (cursor.moveToNext()) {
//先得到name列的角标,再通过角标获取name列的数据
String name =cursor.getString(cursor.getColumnIndex("name"));
String sex =cursor.getString(cursor.getColumnIndex("sex"));
Log.i("MainActivity", "name =" + name +" sex="+sex);
}
}
}