zoukankan      html  css  js  c++  java
  • 寒假学习进度七

    安卓数据库的操作:今天主要学了下安卓数据库方面的简单知识。数据库主要用于复杂的数据储存。

    安卓数据库简介:安卓SQLite数据库是一款轻量级的关系型数据库,它的运算速度非常快,占用资源很 少,通常只需要几百KB的内存就足够了。SQLite不仅支持标 准的SQL语法,还遵循了数据库的ACID事务,所以只要你以前使用过其他的关系型数据库,就 可以很快地上手SQLite。而SQLite又比一般的数据库要简单得多,它甚至不用设置用户名和密码 就可以使用。

    数据库的创建:Android为了让我们能够更加方便地管理数据库,专门提供了一个SQLiteOpenHelper帮助类,借 助这个类就可以非常简单地对数据库进行创建和升级。

    新建一个Java类继承SQLiteOpenHelper类,在主活动中调用该类即可实现对数据库的创建。

    public class MyDatabaseHelper extends SQLiteOpenHelper {
    
        public static final String CREATE_BOOK = "create table book ( " +
                "id integer primary key autoincrement," +
                "author text," +
                "price real," +
                "pages integer," +
                "name text )";
    
        private Context mContext;
    
        public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
            mContext = context;
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_BOOK);
            Toast.makeText(mContext, "数据库成功创建", Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
              
        }
    }
    public class NormalActivity extends AppCompatActivity {
        private MyDatabaseHelper dbHelper;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.normal_layout);
       dbHelper = new MyDatabaseHelper(this, "BookStore.db", null, 1);  
              Button bt3 = findViewById(R.id.create_database);
                 bt3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dbHelper.getWritableDatabase();
                }
            });
        }
    }

    如果想建立多个数据库,由于就需要对数据库进行更新升级,这里建立两个数据库,重写onUpgrade() 方 法。

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop table if exists Book");
    db.execSQL("drop table if exists Category");
    onCreate(db);
    }

    数据库的增改删查

     add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    values.put("name", "暴走大漫画");
                    values.put("author", "张子豪");
                    values.put("pages", 514);
                    values.put("price", 18.9);
                    db.insert("Book", null, values);
                    values.clear();
                    values.put("name", "金刚大力手");
                    values.put("author", "王子顺");
                    values.put("pages", 60);
                    values.put("price", 5);
                    db.insert("Book", null, values);
                    Toast.makeText(NormalActivity.this, "添加数据成功", Toast.LENGTH_SHORT).show();
                }
            });
     update.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    values.put("price", 10);
                    db.update("Book", values, "name=?", new String[]{"暴走大漫画"});
                }
            });
    delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SQLiteDatabase db=dbHelper.getWritableDatabase();
                    db.delete("Book","pages>?",new String[]{"500"});
                }
            });
    query.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SQLiteDatabase db=dbHelper.getWritableDatabase();
                    Cursor cursor=db.query("Book",null,"pages>?",new String[]{"10"},null,null,null,null);
                    if(cursor.moveToFirst()){
                        do{
                            String name=cursor.getString(cursor.getColumnIndex("name"));
                            String author=cursor.getString(cursor.getColumnIndex("author"));
                            int pages=cursor.getInt(cursor.getColumnIndex("pages"));
                            double price= cursor.getDouble(cursor.getColumnIndex("price"));
                            Log.d("数据库查找","书名:"+name);
                            Log.d("数据库查找","作者:"+author);
                            Log.d("数据库查找","页数:"+pages);
                            Log.d("数据库查找","价格:"+price);
                        }while (cursor.moveToNext());
                    }
                    cursor.close();
                }
            });
       

    用命令行查看数据库:

  • 相关阅读:
    【基础算法】- 全排列
    【基础算法】- 2分查找
    区块链培训
    Static Binding (Early Binding) vs Dynamic Binding (Late Binding)
    test
    No data is deployed on the contract address!
    "throw" is deprecated in favour of "revert()", "require()" and "assert()".
    Variable is declared as a storage pointer. Use an explicit "storage" keyword to silence this warning.
    京都行
    Failed to write genesis block: database already contains an incompatible
  • 原文地址:https://www.cnblogs.com/weixiao1717/p/12275194.html
Copyright © 2011-2022 走看看