Skip to content

chud04/SQLite-Simple-Android

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Version - 2.1

Install

You may import src from project or download jar (recommended)

Quick start

Code reference you may see in project

  • Create your model with annotation @Column(type = ColumnType.TYPE), for example:

See all model parameters below in section Model

public class Record {

    public transient static final String COLUMN_RECORD_TEXT = "recordText";
    public transient static final String COLUMN_ID = "_id";

    @Column(name = COLUMN_ID, type = ColumnType.INTEGER, isPrimaryKey = true, isAutoincrement = true)
    private int id;

    @Column(name = COLUMN_RECORD_TEXT, type = ColumnType.TEXT)
    private String recordText;

    // also supports ColumnType.NUMERIC, ColumnType.REAL, ColumnType.BLOB

}
  • Create class extends Application.
public class MainApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        SQLiteSimple databaseSimple = new SQLiteSimple(this);
        databaseSimple.create(Record.class); // enumerate classes Class1.class, Class2.class,...
    }

}
  • Add to AndroidManifest.xml
    <application
        android:name=".MainApplication"
        ...
        ...
            >
  • Create «DAO» class extends SQLiteSimpleDAO<YourModelName>
public class RecordsDAO extends SQLiteSimpleDAO<Record> {

    public RecordsDAO(Context context) {
        super(Record.class, context);
    }

 }

That's all!

In your activity just create operator, for example:

    ...

    private RecordsDAO recordsDAO;

      @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            recordsDAO = new RecordsDAO(this);
       }

      @Override
        protected void onDestroy() {
            super.onDestroy();
            recordsDAO.recycle();
        }

And you may call all needed methods, if you need more, just override or create new in class RecordsDAO, look above.

Model

Look better this nuance, available annotations:

@Table - optional parameter for classes, supports attributes:

name - optional.

@Column - required parameter for variables, also required the type of column, supports attributes:

type - required (ColumnType.TEXT, ColumnType.NUMERIC, ColumnType.REAL, ColumnType.INTEGER, ColumnType.BLOB).

name - optional.

isPrimaryKey - optional (for keys, supports compound key).

isAutoincrement - optional (for key, aka _id).

Database version

**Database version** - if you upgrade database version, for example from 1 to 2, all your tables will be deleted, and created again. **DATA WILL BE LOST.** If you want only add column, just write it on model and SQLite Simple create it for you.

How use:

public class MainApplication extends Application {

    private final static int DATABASE_VERSION = 2;

    @Override
    public void onCreate() {
        super.onCreate();
        SQLiteSimple databaseSimple = new SQLiteSimple(this, DATABASE_VERSION); // just write here
        databaseSimple.create(Record.class);                                    // if you not specify version
    }                                                                           // library set version 1
}

Read database from assets

```java public class MainApplication extends Application {
@Override
public void onCreate() {
    super.onCreate();
    SQLiteSimple databaseSimple = new SQLiteSimple(this, "example.sqlite");
    databaseSimple.create(Example.class);
}

}


<h2>FTS (Full-Text Search)</h2>

```java

    ...

    private SQLiteSimpleFTS simpleFTS;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        simpleFTS = new SQLiteSimpleFTS(this, false); // second parameter is a tables category,
    }                                                 // search between several tables

    @Override
    protected void onDestroy() {
        super.onDestroy();
        simpleFTS.recycle();
    }

That's all! Use methods as described below in appropriate places, like in a example.

simpleFTS.search(...), simpleFTS.create(...).

Tools

Also may be used:

SimpleConstants

ZERO_RESULT - if, for example, object not found, or not created

SimplePreferencesUtil methods:

getDatabaseVersion() - current local database version.

isVirtualTableCreated() - for FTS.

SimpleDatabaseUtil methods:

getFullDatabasePath() - path to internal database.

About

Simple to use library for SQLite in Android

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published