`

持久化技术

 
阅读更多

Android有3种持久化数据的方式:

1.SharedPreferences

2.文件读取

3.数据库

 

SharedPreferences

代码示例:

//将数据保存在Editor中
SharedPreferences.Editor editor= getSharedPreferences("data",MODE_PRIVATE).edit();
editor.putString("name","Tom");
editor.apply();

//取出数据
SharedPreferences sharedPreferences=getSharedPreferences("data",MODE_PRIVATE);
Log.d("testData",sharedPreferences.getString("name",""));

 

 

 

存储数据到文件

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

 

从文件读取数据

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();
}

 

 

SQLite数据库,用LitePal操作

1.AndroidManifest.xml的Application标签中添加一行代码:

   

android:name="org.litepal.LitePalApplication"

 

2.创建assets目录,添加一个litepal.xml文件

    <list>中的内容是映射对象的路径,根据实际情况填写

<?xml version="1.0" encoding="utf-8"?>
<litepal>
    <dbname value="BookStore" ></dbname>

    <version value="2" ></version>

    <list>
        <mapping class="com.example.litepaltest.Book"></mapping>
    </list>
</litepal>

 

3.创建javabean类,注意extends DataSupport

public class Book extends DataSupport {
//添加属性,并添加setter(),getter()方法 
   private int id;
    private String author;
    private double price;
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

 

4.创建数据库

  

Connector.getDatabase();

 

5.添加数据

Book book = new Book();
book.setAuthor("1");
book.setPrice(1);
book.save();

 

6.更新id=1的数据

Book book = new Book();
book.setAuthor("2");
book.setPrice(1);
book.update(1);

 

7.删除数据

DataSupport.deleteAll(Book.class, "price < ?", "15"); //删除Book表中,价格小于15的数据

  

DataSupport.deleteAll(Book.class);     //删除整个表

 

8.查询数据

List<Book> books = DataSupport.findAll(Book.class);
for (Book book: books) {
    Log.d("MainActivity", "book ID is " + book.getId());
    Log.d("MainActivity", "book author is " + book.getAuthor());
    Log.d("MainActivity", "book price is " + book.getPrice());
}

 

  

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics