I currently have a DatabaseHelper class in my code (extending SQLiteOpenHelper) which creates a table and inserts values into it. The problem with this however is that my app will store data of various users, which can be accessed by users - so doing it this way means that if (for instance) I registered an account in my app on one device, that account wouldn't exist on any other device (since the database is stored in the operating system itself).
For now, I want to use my laptop as a server for the database so that I will be able to log into the same account using any Android device. I installed sqlite on my desktop but I don't have a GUI or anything with it (and am really unsure how to use it). I already have logic for creating a table / inserting data etc. in code - would it be possible to keep this code but store the data somewhere on my laptop instead?
Here is the current code I have:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Accounts";
public static final String TABLE_NAME = "account_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "USERNAME";
public static final String COL_3 = "EMAIL";
public static final String COL_4 = "PASSWORD";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, USERNAME TEXT, EMAIL TEXT, PASSWORD TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_NAME);
onCreate(db);
}
public boolean insertData(String username, String emailAddress, String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL_2, username);
cv.put(COL_3, emailAddress);
cv.put(COL_4, password);
long result = db.insert(TABLE_NAME, null, cv);
return (result != -1);
}
public int Login(String username,String password)
{
String[] selectionArgs = new String[]{username, password};
try
{
int i = 0;
Cursor c = null;
SQLiteDatabase db = this.getReadableDatabase();
c = db.rawQuery("select * from " + TABLE_NAME + " where username=? and password=?", selectionArgs);
c.moveToFirst();
i = c.getCount();
c.close();
return i;
}
catch(Exception e)
{
e.printStackTrace();
}
return 0;
}
}
Thanks in advance for any insight!
Aucun commentaire:
Enregistrer un commentaire