Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
StreetGuardian/SQLDatabaseExample
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
75 lines (65 sloc)
2.96 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
import android.util.Log; | |
public class DBHandler extends SQLiteOpenHelper { | |
protected static final String COLUMN_ID = "User_ID"; | |
private static final String DATABASE_NAME = "Database.db"; | |
private static final int DATABASE_VERSION = 1; | |
protected static final String TABLE_NAME = "User"; | |
//question table connected to username of previous table | |
protected static final String TABLE_NAME2 = "Question"; | |
protected static final String COLUMN_USER = "Username"; | |
protected static final String COLUMN_PASS = "Password"; | |
protected static final String COLUMN_USER_ID = "User_ID"; | |
protected static final String COLUMN_QUESTIONS = "QuestionTitle"; | |
protected static final String COLUMN_ANSWER = "Answer"; | |
protected static final String TABLE_NAME3 = "Note"; | |
protected static final String COLUMN_NOTE_ID = "ID"; | |
protected static final String COLUMN_NOTES = "Notes"; | |
//information fot Table 1 - login details | |
private static final String TAG = "DataB"; | |
//initialize the database | |
DBHandler(Context context) { | |
super(context, DATABASE_NAME, null, DATABASE_VERSION); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
String createTable = "CREATE TABLE " + TABLE_NAME + " (" | |
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " | |
+ COLUMN_USER + " TEXT NOT NULL, " | |
+ COLUMN_PASS + " TEXT NOT NULL " | |
+ ");"; | |
String createTable2 = "CREATE TABLE " + TABLE_NAME2 + " (" | |
+ COLUMN_USER_ID + " INTEGER, " | |
+ COLUMN_QUESTIONS + " TEXT, " | |
+ COLUMN_ANSWER + " TEXT NOT NULL" | |
+ ")"; | |
String createTable3 = "CREATE TABLE " + TABLE_NAME3 + " (" | |
+ COLUMN_NOTE_ID + " INTEGER, " | |
+ COLUMN_NOTES + " TEXT PRIMARY KEY" | |
+ ");"; | |
db.execSQL(createTable); | |
db.execSQL(createTable2); | |
db.execSQL(createTable3); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int i, int i1) { | |
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); | |
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME2); | |
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME3); | |
onCreate(db); | |
} | |
public long addData(String user, String pass) { | |
SQLiteDatabase db = this.getWritableDatabase(); | |
ContentValues values = new ContentValues(); | |
values.put(COLUMN_USER, user); | |
values.put(COLUMN_PASS, pass); | |
Log.d(TAG, "addData: Adding " + user + " to " + TABLE_NAME); | |
Log.d(TAG, "addData: Adding " + pass + " to " + TABLE_NAME); | |
long result = db.insert(TABLE_NAME, null, values); | |
//if data is invalid it will return -1 | |
return result; | |
} | |
} |