

** Called when the activity is first created. Public class AndroidSQLiteActivity extends Activity SQLiteDatabase db = this.getReadableDatabase() Ĭursor cursor = db.query(TABLE_User, new String ) Getting single User by ID User getUser(int id) Values.put(KEY_PH_NO, User.getPhoneNumber()) // User Phoneĭb.close() // Closing database connection Values.put(KEY_NAME, User.getName()) // User Name SQLiteDatabase db = this.getWritableDatabase() ĬontentValues values = new ContentValues() The current database version to check if you must add some changes void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)ĭb.execSQL("DROP TABLE IF EXISTS " + TABLE_UserS) Īll CRUD(Create, Read, Update, Delete) Operations Used when you want to change a column type or add new field. + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT," String CREATE_User_TABLE = "CREATE TABLE " + TABLE_User+ "(" Private static final String KEY_PH_NO = "phone_number" Ĭreating Table USER void onCreate(SQLiteDatabase db) Private static final String KEY_NAME = "name" Private static final String KEY_ID = "id"


Private static final String TABLE_User = "User" Private static final String DATABASE_NAME = "DBUsers" Private static final int DATABASE_VERSION = 1 Return "User " ĭeclare static variables // Database Version Public void setPhoneNumber(String phone_number) Public User(String name, String _phone_number) Public User(int id, String name, String _phone_number) This table contains three fields: id (INT), name (TEXT), phone_number(TEXT). In this tutorial we will implement an example of storing users in the SQLite database. The class contains the methods: moveToFirst(), moveToNext(), moveToPrevious() and moveToPosition().
#Open sqllite database with sql studio android#
To navigate on the queried database records Android provides a class called Cursor. The SQLiteDatabase object can then use the standard insert(), update(), delete(), and query() operations to manipulate the records within the database table. This object uses the DatabaseHelper object to open the writable database. The SQLiteDatabase class is used to create a SQLiteDatabase object. In order to be used, the class SQLiteOpenHelper must be extended by a class from the app. The method onUpgrade() is used when the structure of a database table is changed, for example when a new field is added or the type of a field is changed. The method onCreate() is the one used when the database table is created. This class is used for opening, creating and upgrading the database. All of the operations regarding for inserting, updating, and deleting records are in the DBAdapter.
#Open sqllite database with sql studio code#
This class is called DBAdapter and contains all the necessary code for creating the tables and the assigned fields. When implementing a database on Android, a common practice is to create a class which interacts with the SQLite database and also with the activities of the app. In order to access this database it must be used the API provided by Android which is available in the package. SQLite supports all the relational database features common on the Oracle databases or Microsoft SQL Server. SQLite is one way of storing the application data, locally, on the device. Android provides several ways to store user and app data.
