如图:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="add"
- android:onClick="add"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="delete"
- android:onClick="delete"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="alter"
- android:onClick="alter"/>
- <Button
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="query"
- android:onClick="query"/>
-
- <ListView
- android:id="@+id/lv_db"
- android:layout_width="match_parent"
- android:layout_height="match_parent"/>
- </LinearLayout>
-
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
-
-
- <TextView
- android:id="@+id/tv_name"
- android:layout_width="match_parent"
- android:layout_weight="1"
- android:textSize="30dp"
- android:layout_height="wrap_content"
- android:text=""/>
- <TextView
- android:id="@+id/tv_phone"
- android:layout_width="match_parent"
- android:layout_weight="1"
- android:textSize="30dp"
- android:layout_height="wrap_content"
- android:text=""/>
- </LinearLayout>
-
- public class MyDBOpenHelper extends SQLiteOpenHelper {
-
- private final String DB_FOOD = "create table food(_id integer primary key autoincrement,name varchar(20),phone varchar(20))";
-
- public MyDBOpenHelper( Context context) {
- super(context, "food.db", null, 1); }
-
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL(DB_FOOD);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- }
- }
-
onupgrade这个方法只有版本号比之前大的时候才会执行
alt+insert之后选择
- public class Food {
- private String name;
- private String phone;
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getPhone() {
- return phone;
- }
-
- public void setPhone(String phone) {
- this.phone = phone;
- }
- }
-
(具体方法之前的文章有介绍)ListView简单使用实例
- public class MyAdapter extends BaseAdapter {
- private Context context;
- private List<Food> foodList;
- public MyAdapter(Context context,List<Food>foodList){
- this.context=context;
- this.foodList=foodList;
- }
- @Override
- public int getCount() {
- return foodList.size();
- }
-
- @Override
- public Object getItem(int position) {
- return null;
- }
- @Override
- public long getItemId(int position) {
- return 0;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder;
- if (convertView==null){
- convertView=View.inflate(context,R.layout.item,null);
- holder = new ViewHolder();
- holder.tv_name=(TextView)convertView.findViewById(R.id.tv_name);
- holder.tv_phone=(TextView)convertView.findViewById(R.id.tv_phone);
- convertView.setTag(holder);
- }else {
- holder = (ViewHolder) convertView.getTag();
- }
- holder.tv_name.setText(foodList.get(position).getName());
- holder.tv_phone.setText(foodList.get(position).getPhone());
- return convertView;}
- private static class ViewHolder{
- TextView tv_name;
- TextView tv_phone;
- }
- }
-
- public class MainActivity extends AppCompatActivity {
- private MyDBOpenHelper myDBOpenHelper;
- private ListView lv_db;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- myDBOpenHelper = new MyDBOpenHelper(this);
- lv_db = (ListView) findViewById(R.id.lv_db);
- }
- public void add(View view) {
- SQLiteDatabase db = myDBOpenHelper.getReadableDatabase();
- ContentValues values = new ContentValues();
- values.put("name", "苹果");
- values.put("phone", "666");
- long result = db.insert("food", null, values);
- if (result > 0)
- Toast.makeText(this, "success", Toast.LENGTH_SHORT).show();
- else
- Toast.makeText(this, "fail", Toast.LENGTH_SHORT).show();
- db.close();
- }
- public void delete(View view) {
- SQLiteDatabase db = myDBOpenHelper.getReadableDatabase();
- int result = db.delete("food", "1", null);
- if (result > 0)
- Toast.makeText(this, "delete" + result + "row", Toast.LENGTH_SHORT).show();
- else
- Toast.makeText(this, "delete 0 row", Toast.LENGTH_SHORT).show();
- db.close();
- }
- public void alter(View view) {
- SQLiteDatabase db = myDBOpenHelper.getReadableDatabase();
- ContentValues values = new ContentValues();
- values.put("name", "葡萄");
- int result = db.update("food", values, "phone=?", new String[]{"666"});
- if (result > 0)
- Toast.makeText(this, "alter" + result + "rows", Toast.LENGTH_SHORT).show();
- else
- Toast.makeText(this, "alter 0 row", Toast.LENGTH_SHORT).show();
- db.close();
- }
- public void query(View view) {
- ArrayList<Food> foodList = new ArrayList<>();
- SQLiteDatabase db = myDBOpenHelper.getReadableDatabase();
- Cursor cursor = db.query("food", new String[]{"name", "phone"}, null, null, null, null, null);
-
- if (cursor != null && cursor.getCount() > 0) {
- while (cursor.moveToNext()) {
- String name = cursor.getString(0);/*开始读*/
- String phone = cursor.getString(1);
- Food food = new Food();
- food.setName(name);
- food.setPhone(phone);
- foodList.add(food);
- Log.v("MainActivity_query", "query:name=" + name + "/phone=" + phone);/*显示*/
- }
- }
- cursor.close();
- db.close();
- MyAdapter myAdapter = new MyAdapter(this,foodList);
- lv_db.setAdapter(myAdapter);
- }
- }
-
点击add,显示
点击delete,显示
点击alter,显示
execsql没有返回值 android api语句有返回值 可以判断成功不成功
api只能做单表操作 不能对表的结构增删改查