java Android中的SQLite数据库创建和数据库读/写操作。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Android中的SQLite数据库创建和数据库读/写操作。相关的知识,希望对你有一定的参考价值。


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;


public class MyDatabase {

    private SQLiteDatabase database;
    private DatabaseNameDbHelper helper;

    public MyDatabase(Context context) {
        helper = new DatabaseNameDbHelper(context);
    }

    private SQLiteDatabase openReadableDatabaseInstance() {
        return helper.getReadableDatabase();
    }

    private SQLiteDatabase openWritableDatabaseInstance() {
        return helper.getWritableDatabase();
    }

    private void closeDatabaseConnection() {
        database.close();
        helper.close();
    }



    public long updateTableName(long id, String column1, int column2){
        
        database = openWritableDatabaseInstance();
        
        ContentValues contentValues = new ContentValues();
        contentValues.put(TableNameEntry.COLUMN_1_NAME, column1);
        contentValues.put(TableNameEntry.COLUMN_2_NAME, column2);


        String selection = TableNameEntry._ID +" = ? ";
        String[] selectionArgs = {String.valueOf(id)};

        long value = database.update(TableNameEntry.TABLE_NAME, contentValues, selection, selectionArgs);
        closeDatabaseConnection();
        
        return value;
    }

    
    public long deleteAllTableDetails() {
        database = openWritableDatabaseInstance();
      
        long l= database.delete(TableNameEntry.TABLE_NAME, null, null);
        
        closeDatabaseConnection();
        
        return l;
    }

    public ArrayList<Table> getTableDataInArrayList() {
        database = openReadableDatabaseInstance();
        
        Cursor c =  database.query(TableNameEntry.TABLE_NAME, null, null, null, null, null, null);
        
        ArrayList<Table> arrayListTables = new ArrayList<>();

        if(c.moveToFirst()){
            do{
                //String stringDate = Utils.SIMPLE_DATE_FORMAT.format(date.getTime());
                Table table = new Table(c.getInt(c.getColumnIndex(TableNameEntry._ID)),
                c.getString(c.getColumnIndex(TableNameEntry.COLUMN_1_NAME)),
                c.getInt(c.getColumnIndex(TableNameEntry.COLUMN_2_NAME)));
                arrayListTables.add(table);
            }while (c.moveToNext());
        }
        closeDatabaseConnection();

        return arrayListTables;
    }



    public long createRowTableNameDetails(String column1, int column2) {
      
        database = openWritableDatabaseInstance();

        ContentValues contentValues = new ContentValues();
        contentValues.put(TableNameEntry.COLUMN_DATE, column1);
        contentValues.put(MatchDetailsEntry.COLUMN_TIME, column2);
        long value = database.insert(TableNameEntry.TABLE_NAME, null, contentValues);
        
        closeDatabaseConnection();
        
        return value;
        
    }

    private class DatabaseNameDbHelper extends SQLiteOpenHelper {

        //region SQL Statements
        private static final String SQL_CREATE_TABLE_NAME_TABLE = "CREATE TABLE " + TableNameEntry.TABLE_NAME + "("
                + TableNameEntry._ID + " INTEGER PRIMARY KEY, "
                + TableNameEntry.COLUMN_1_NAME + " TEXT NOT NULL, "
                + TableNameEntry.COLUMN_2_NAME + " INT NOT NULL);"

        private static final String SQL_DROP_TABLE_NAME_TABLE = "DROP TABLE " + TableNameEntry.TABLE_NAME + ";";
        //endregion

        private static final String DATABASE_NAME = "DatabaseName.db";

        private static final int DATABASE_VERSION = 1;

        public DatabaseNameDbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(SQL_CREATE_TABLE_NAME_TABLE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            if (newVersion > oldVersion) {
                db.execSQL(SQL_DROP_TABELE_NAME_TABLE);
                onCreate(db);
            }
        }
    }


}

import android.provider.BaseColumns;


public final class DatabaseNameContract {
  
  private DatabaseNameContract(){
    
  }

    public static final class TableNameEntry implements BaseColumns {
        public static final String TABLE_NAME = "tableName";
        public static final String COLUMN_1_NAME = "column1Name";
        public static final String COLUMN_2_NAME = "column2Name";
    }
}

以上是关于java Android中的SQLite数据库创建和数据库读/写操作。的主要内容,如果未能解决你的问题,请参考以下文章

Android 中的数据库初始总结(SQLite部分)

Android,删除列表和sqlite java中的项目

无法在 android studio SQLite 数据库中动态创建多个表

将大型数组缓存到 SQLite - Java/Android

如何从 Sqlite ( android ) 中的 datetime 字段获取日期

详解Android中的SQLite数据库存储