Android has 3 ways to persist data:
1.SharedPreferences
2. File reading
3. Database
SharedPreferences
Code example:
// save the data in the Editor SharedPreferences.Editor editor= getSharedPreferences("data",MODE_PRIVATE).edit(); editor.putString("name","Tom"); editor.apply(); //fetch data SharedPreferences sharedPreferences=getSharedPreferences("data",MODE_PRIVATE); Log.d("testData",sharedPreferences.getString("name",""));
Store data to file
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 (); }
read data from file
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 database, operate with LitePal
1. Add a line of code to the Application tag of AndroidManifest.xml:
android:name="org.litepal.LitePalApplication"
2. Create the assets directory and add a litepal.xml file
The content in <list> is the path of the mapping object, fill in according to the actual situation
<?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. Create a javabean class, pay attention to extends DataSupport
public class Book extends DataSupport { //Add properties, and add setter(), getter() methods 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. Create the database
Connector.getDatabase();
5. Add data
Book book = new Book(); book.setAuthor("1"); book.setPrice(1); book.save();
6. Update the data with id=1
Book book = new Book(); book.setAuthor("2"); book.setPrice(1); book.update(1);
7. Deletion of data
DataSupport . deleteAll (Book. class , "price < ?" , "15" ); //Delete the data in the Book table whose price is less than 15
DataSupport. deleteAll (Book. class ); // delete the entire table
8. Query data
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()); }