public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity"; private MyDatabaseHelper myDatabaseHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
//增加version 来删除之前的表信息 myDatabaseHelper = new MyDatabaseHelper(this,"peopleInfo.db",null,9); //创建数据库 myDatabaseHelper.getWritableDatabase();
//打开数据库 SQLiteDatabase database = myDatabaseHelper.getWritableDatabase(); //新增zhangsan 18 11.11 // ContentValues values = new ContentValues(); // values.put("name","zhangsan"); // values.put("age",18); // values.put("price",11.11); // database.insert("people",null,values);
database.execSQL("insert into people (name,age,price) values(?,?,?)",new String[] {"zhangsan","18","11.11"});
//新增lisi 28 22.22 // values.clear(); // values.put("name","lisi"); // values.put("age",28); // values.put("price",22.22); // database.insert("people",null,values);
database.execSQL("insert into people (name,age,price) values(?,?,?)",new String[] {"lisi","28","22.22"});
//新增wangwu 48 44.44 // values.clear(); // values.put("name","wangwu"); // values.put("age",48); // values.put("price",44.44); // database.insert("people",null,values);
database.execSQL("insert into people (name,age,price) values(?,?,?)",new String[] {"wangwu","48","44.44"});
//删除zhangsan信息 // database.delete("people","name = ?",new String[] {"zhangsan"});
database.execSQL("delete from people where name = ?",new String[] {"zhangsan"});
//更改lisi age=38 // values.clear(); // values.put("age",38); // database.update("people",values,"name = ?",new String[] {"lisi"});
database.execSQL("update people set age = ? where name = ? ",new String[] {"38","lisi"});
//查询people表的内容并打印 // Cursor cursor = database.query("people",null,null,null,null,null,null,null); Cursor cursor = database.rawQuery("select * from people",null); if (cursor.moveToFirst()){ do{ String name = cursor.getString(cursor.getColumnIndex("name")); int age = cursor.getInt(cursor.getColumnIndex("age")); double price = cursor.getDouble(cursor.getColumnIndex("price"));
Log.d(TAG, "onCreate: "+name+" "+age+" "+price);
}while (cursor.moveToNext());
} cursor.close();
} }
|