Storing constructors in SQLite - java

I want to store constructors or references to constructors in an SQL table to look up a class name and be able to access a constructor and instantiate from there. In C I would just store a pointer to a static function; how do I do it in Java? Any ideas?
// Creating Tables
#Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_CONSTRUCTOR + " ///SOMETHING THAT CAN DESCRIBE A CONSTRUCTOR///" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}

Related

Database does not store data. I get the "SQLiteLog: (1) near "Name": syntax error" from Android Studio log

I get from "SQLiteLog: (1) near "Name": syntax error" after the addPatient() method is called and the data given by addPatient() method is not stored in the database.
At first, I suspect that something might be wrong with my "CREATE TABLE" query but I have tried everything and I couldn't figure out what was wrong.
#Override
public void onCreate(SQLiteDatabase db)
{
//===============Create a table for Patient
String query = "CREATE TABLE TABLE_PATIENT (COLUMN_ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"COLUMN_USERNAME TEXT, " +
"COLUMN_PASSWORD TEXT, " +
"COLUMN_FIRSTNAME TEXT, " +
"COLUMN_LASTNAME TEXT, " +
"COLUMN_AGE TEXT, " +
"COLUMN_GENDER TEXT, " +
"COLUMN_PHONE TEXT, " +
"COLUMN_ADDRESS TEXT);";
db.execSQL(query);
}//End of onCreate()
//Add a new Patient Row to the database
public void addPatient(Patient patient)
{
Log.i(TAG, "addPatient("+patient.getUserName()+")");
ContentValues values = new ContentValues();
values.put(COLUMN_ID, patient.getU_Id());
values.put(COLUMN_USERNAME, patient.getUserName());
values.put(COLUMN_PASSWORD, patient.getPassword());
values.put(COLUMN_FIRSTNAME, patient.getFirstName());
values.put(COLUMN_LASTNAME,patient.getLastName());
values.put(COLUMN_AGE, patient.getAge());
values.put(COLUMN_GENDER,patient.getGender());
values.put(COLUMN_PHONE,patient.getPhoneNumber());
values.put(COLUMN_ADDRESS, patient.getAddress());
SQLiteDatabase db = getWritableDatabase();
try
{
db.insert(TABLE_PATIENT, null, values);
db.close();
}catch (Exception e)
{
Log.i(TAG, e.getMessage());
}
}//End of addPatient()
I guess you have defined these variables:
String COLUMN_ID = "id";
String COLUMN_USERNAME = "username";
....................................
or something like that.
If you have spaces in the names of the columns you must use square brackets or backticks around them, like:
String COLUMN_USERNAME = "[user name]";
In your CREATE TABLE statement you define the column names as:
"COLUMN_ID", "COLUMN_USERNAME", ....
because you use the variable names and not their values.
But in addPatient() method you are putting values to the ContentValues object by using their actual names.
To solve your problem, 1st uninstall the app from the device you are testing it, so the database is deleted.
Then change the CREATE TABLE statement like this:
String query = "CREATE TABLE " + TABLE_PATIENT +" (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_USERNAME + " TEXT, " +
COLUMN_PASSWORD + " TEXT, " +
COLUMN_FIRSTNAME + " TEXT, " +
COLUMN_LASTNAME + " TEXT, " +
COLUMN_AGE + " TEXT, " +
COLUMN_GENDER + " TEXT, " +
COLUMN_PHONE + " TEXT, " +
COLUMN_ADDRESS + " TEXT)";
and rerun to recreate the database with the correct column names.

HttpReader OnResultReadyListener: attempt to re-open an already-closed object: SQLiteDatabase

I'm trying to convert a csv file that I get online and try to call a execSQL(String) based on the results.
The only problem is dat I send my SQLiteDatabase with my function in the parameter, but that way I can't acces it from within my resultReady(String result){} if I don't make the parameter final.
But when I make the SQLiteDatabase parameter final, I get the error message:
"attempt to re-open an already-closed object: SQLiteDatabase: ..."
Does anyone know how I could fix this?
The function:
private void insertTalen(SQLiteDatabase db) {
// lijst van alle talen met taalcode opgehaald van het internet
HttpReader httpReader = new HttpReader();
httpReader.setOnResultReadyListener(new HttpReader.OnResultReadyListener() {
#Override
public void resultReady(String result) {
CsvHelper csvHelper = new CsvHelper();
List<Taal> talen = csvHelper.getTalenFromCsv(result);
for (Taal taal: talen) {
String SQLScript = "INSERT INTO taal (engelseTaalNaam, taalCode) VALUES ('";
SQLScript += taal.getEngelseNaam();
SQLScript += "', '";
SQLScript += taal.getTaalCode();
SQLScript += "');";
db.execSQL(SQLScript);
}
}
});
httpReader.execute("https://raw.githubusercontent.com/datasets/language-codes/master/data/language-codes.csv");
}
And this function is called from my oncreate:
#Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE_TAAL = "CREATE TABLE taal (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"engelseNaam TEXT," +
"taalCode TEXT)";
db.execSQL(CREATE_TABLE_TAAL);
String CREATE_TABLE_GEBRUIKER = "CREATE TABLE gebruiker (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"highscoreGebruikerId INTEGER," +
"eigenTaalId INTEGER," +
"vreemdeTaalId INTEGER," +
"FOREIGN KEY (eigenTaalId) REFERENCES taal(id)," +
"FOREIGN KEY (vreemdeTaalId) REFERENCES taal(id))";
db.execSQL(CREATE_TABLE_GEBRUIKER);
String CREATE_TABLE_OPGESLAGEN_WOORD = "CREATE TABLE opgeslagenWoord (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
"taalId INTEGER," +
"naam TEXT," +
"engelseVersieId INTEGER," +
"FOREIGN KEY (taalId) REFERENCES taal(id)," +
"FOREIGN KEY (engelseVersieId) REFERENCES opgeslagenWoord(id))";
db.execSQL(CREATE_TABLE_OPGESLAGEN_WOORD);
String CREATE_TABLE_GEKEND_WOORD = "CREATE TABLE gekendWoord (" +
"opgeslagenWoordId INTEGER PRIMARY KEY ," +
"datumverloopt TEXT," +
"niveauGekend INTEGER," +
"FOREIGN KEY (opgeslagenWoordId) REFERENCES opgeslagenWoord(id))";
db.execSQL(CREATE_TABLE_GEKEND_WOORD);
this.db = db;
insertTalen(db);
insertGebruiker(db);
}

Autoincrement not working on ID

I'm having some trouble getting my autoincrement to work in SQL in my android app.
public static final String TABLE_NAME = "collections";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TITLE = "title";
public static final String CREATE_STATEMENT = "CREATE TABLE " + TABLE_NAME +
"(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
COLUMN_TITLE + " TEXT NOT NULL" + ")";
Are there any major problems with the create statement that could cause this?
There is no need to add AUTOINCREMENT NOT NULL to the primary key, As primary key(column) is supposed to be auto-increment and it can't be null.
following is an example of a SQL query which you may edit to create your own table.
public static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + "Your_Table_Name" +
" (" + BaseColumns._ID + " INTEGER PRIMARY KEY," +
"first_column_name" + " TEXT" + "," +
"second_column_name" + " TEXT" + ")";
and BaseColumns is in interface provided by android in package android.provider which you may use to create a primary column with name "_id"
When you insert a new value do not define id (it will be automatically generated) just use COLUMN_TITLE.
Even in scheme you can skip id definition, because sqlite use RAWID internally and latter aliases your ID to RAWID.

How can I set primary key in particular text field?

My Code here
db.execSQL("create table if not exists data(patient_name varchar,patient_id INTEGER PRIMARY KEY ,patient_phone varchar,patient_mail varchar,patient_age,patient_gender varchar,patient_address varchar,patient_lvd varchar,patient_nvd varchar,patient_notes varchar)");
Try this way,
String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "
+ DATABASE_TABLE + "("
+ patient_id + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ patient_phone + " TEXT, "
+ patient_name + " TEXT, "
+ patient_phone + " TEXT )";
db.execSQL(CREATE_TABLE);
You can follow SQLite Database Tutorial

Inserting a date and time

I'm trying to insert the values from a date picker and a time pickers in my sqlite database. I have stored the date and time in object variables and then call a method in my DBhandler class to insert them through a contentvalue object.
Now where I'm failing here is with the actual insertion and data types.
I'm getting an error on the following code for the 'timeApp' and 'dateApp' I'm passing these variables to the createAppointmentEntry' method as DATE and TIME variables.:
Error message:
The method put(String, String) in the type ContentValues is not applicable for the arguments (String, Time)
Method the errors occuring on:
public void createAppointmentEntry(String nameApp, String typeApp, Time timeApp, Date dateApp ,String commentApp, Boolean onOrOff) {
ContentValues cv = new ContentValues();
cv.put(KEY_NAMEAPP, nameApp);
cv.put(KEY_TYPEAPP, typeApp);
//ERROR on the following two 'puts'
cv.put(KEY_TIMEAPP, timeApp);
cv.put(KEY_DATEAPP, dateApp);
cv.put(KEY_COMMENTAPP, commentApp);
cv.put(KEY_ALARM, onOrOff);
ourDatabase.insert(DATABASE_TABLE, null, cv);
Heres my DB columns:
public static final String KEY_ROWAPPID = "_appid";
public static final String KEY_NAMEAPP = "app_name";
public static final String KEY_TYPEAPP = "app_type";
public static final String KEY_TIMEAPP = "app_time";
public static final String KEY_DATEAPP = "app_date";
public static final String KEY_COMMENTAPP = "app_comments";
public static final String KEY_ALARM = "app_alarm";
My onCreate method:
db.execSQL("CREATE TABLE " + DATABASE_TABLEAPP + " (" +
KEY_ROWAPPID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAMEAPP + " TEXT NOT NULL, " +
KEY_TYPEAPP + " TEXT NOT NULL, " +
KEY_TIMEAPP + " TEXT NOT NULL, " +
KEY_DATEAPP + " TEXT NOT NULL, " +
KEY_COMMENTAPP + " TEXT NOT NULL, " +
KEY_ALARM + "BOOLEAN NOT NULL);"
);
Heres how I am setting the time and date object variables:
Date setDate = new Date(dobYear - 1900, dobMonth, dobDate);
Time timeToSet = new Time();
timeToSet.set(0, dobMinute, dobHour);
Judging from the compilation error and the API of ContentValues, it seems that ContentValues only supports primitive types, String and byte[]. So, try replacing:
//ERROR on the following two 'puts'
cv.put(KEY_TIMEAPP, timeApp);
cv.put(KEY_DATEAPP, dateApp);
with:
cv.put(KEY_TIMEAPP, timeApp.toString());
cv.put(KEY_DATEAPP, dateApp.toString());
You can use formatter to format the Time and Date to a standard String to be able to parse it back.
i guess something but i am not sure
KEY_ALARM + "BOOLEAN NOT NULL);"
you missed a whitespace at the create Statement. Try droping it and recreate it same just with the whitespace.
db.execSQL("CREATE TABLE " + DATABASE_TABLEAPP + " (" +
KEY_ROWAPPID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAMEAPP + " TEXT NOT NULL, " +
KEY_TYPEAPP + " TEXT NOT NULL, " +
KEY_TIMEAPP + " TEXT NOT NULL, " +
KEY_DATEAPP + " TEXT NOT NULL, " +
KEY_COMMENTAPP + " TEXT NOT NULL, " +
KEY_ALARM + " BOOLEAN NOT NULL);"
KEY_ALARM + " BOOLEAN NOT NULL);"
Made the same misstage yesterday.
if you wrote a Helperclass just change the DATABASE_VERSION Else call the db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLEAPP);
the Helper class would call this for example
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLEAPP);
onCreate(db); //recreate
}
Automaticaly recreate the database with the new statement than.

Categories