首页 > 代码库 > SQLite数据库

SQLite数据库

数据持久化:

1、文件存储

  适合用于存储一些简单的文本数据或二进制数据

  存储数据:openFileOutput(文件名,操作模式),返回值为一个FileOutputStream对象,借助FileOutputStream对象构建出OutputStreamWriter对象,再借助OutputStreamWriter构建出一个BufferedWriter对象,通过bufferedWriter对象将文本写入到文件中。

public void save(){
  String data = "http://www.mamicode.com/data to save"
  FileOutputStream out = null;
  BufferedWriter writer = null;
  try{
    out = openFileOutput("data",Context.MODE_PRIVATE);
    writer = new BufferedWriter(new OutputStreamWriter(out));
    writer.write(data);
  }catch(IOException e){
    e.printStackTrace();
  }finally{
    try{
      if(writer != null){
        writer.close();
      }catch(IOException e){
        e.printStackTrace();
      }
    }
  }
   }

  读取数据:openFileInput(文件名),返回FileInputStream对象。

Public String load(){
  FileInputStream in = null;
  BufferedReader reader = null;
  StringBuilder content = new StringBuilder();
  try{
    in = openFileInput("data");
    reader = new BufferedReader(new InputStreamReader(in));
    String line = "";
    while((line = reader.readLine())!=null){
      content.append(line);
    }
  }catch(IOException e){
    e.printStackTrace();
  }finally{
    if(reader != null){
      try{
        reader.close();
      }catch(IOException e){
        e.printStackTrace();
      }
    }
  }
  return content.toString(); }

 

2、SharedPreferences存储

  键值对。

  获得SharePreferences对象那个的方法

  (1)getSharedPreferences(文件名,操作模式)

  (2)getPreferences(文件名)

  (3)getDefaultSharePreferences(Context)

  存储数据:

  (1)调用edit()获取一个SharedPreferences.Editor对象

  (2)向SharedPreferences.Editor中添加数据

  (3)调用commit()提交数据

  读取数据:

  getXXX(键,默认值)方法

 

SQLite数据库

  创建:通过SQLiteOpenHelper帮助类实现创建和升级。

     getReadableDatabase()和getWritableDatabase()

  DOS窗口操作SQLite数据库?
  1)adb shell
  2)ls
  3)cd 数据表的路径
  4)sqlite3 数据库完整的名字
  4.1) .tables
  4.2) select * from notetab;
  4.3) .exit
  4.4) .help

  升级:更新版本号

增删改查

  增:insert(表名,null,ContentValues对象)

  改:updata(表名,ContentValues对象,某一行或几行的数据)

  删:delete(表名,某一行或某几行的数据)

  查:query(表名,哪列,某一行或几行的数据,group by的列,过滤,排序方式),返回一个Cursor对象

事务:保证一系列的操作要么完成,要么一个都不完成。

  (1)调用beginTransaction()开启事务

  (2)在异常捕获代码块中执行具体的数据库操作

  (3)调用setTransactionSuccessful()表示执行成功

  (4)endTransaction()结束事务

public class MainActivity extends Activity {

    private MyDatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2);
        Button createDatabase = (Button) findViewById(R.id.create_database);
        createDatabase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dbHelper.getWritableDatabase();
            }
        });

        Button addData = http://www.mamicode.com/(Button) findViewById(R.id.add_data);"name","The Da Vinci Code");
                values.put("author","Dan Brown");
                values.put("pages","100");
                values.put("price","30");
                db.insert("Book",null,values);
                values.clear();

                values.put("name","世界末日");
                values.put("author","死神");
                values.put("pages","10000");
                values.put("price","50000");
                db.insert("Book",null,values);
            }
        });
        Button updataData = http://www.mamicode.com/(Button) findViewById(R.id.updata_data);"price","200");
                db.update("Book",values,"name = ?",new String[]{"Tea Da Vinci Code"});
            }
        });
        Button deleteData = http://www.mamicode.com/(Button) findViewById(R.id.delete_data);"Book","pages<?",new String[]{"450"});
            }
        });
        Button quertData = http://www.mamicode.com/(Button) findViewById(R.id.query_data);"Book",null,null,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.i("XYZ","book name is"+name);
                        Log.i("XYZ","book author name is"+author);
                        Log.i("XYZ","book pages is"+pages);
                        Log.i("XYZ","book price is"+price);
                    }while (cursor.moveToNext());
                }
                cursor.close();
            }
        });
        Button replaceData = http://www.mamicode.com/(Button) findViewById(R.id.replace_data);"Book",null,null);
                if (true){
                    throw new NullPointerException();
                }

                ContentValues values = new ContentValues();
                values.put("name","游戏大全");
                values.put("author","老三");
                values.put("pages","20");
                values.put("price","35.5");
                db.insert("Book",null,values);
                db.setTransactionSuccessful();  //事务执行成功
                db.endTransaction();    //结束事务
            }
        });

    }
}

  

 

SQLite数据库