0

I am working for first time in SQLite, i have the next code for my database:

public class SqlLiteDataBase extends SQLiteOpenHelper{
String sqlCreate = "CREATE TABLE lol (code INTEGER, name TEXT)";

public SqlLiteDataBase(Context context, String name, CursorFactory factory, int version) {
    super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(sqlCreate);
}

@Override
public void onUpgrade(SQLiteDatabase db, int versionAnterior, int versionNueva) {
    db.execSQL("DROP TABLE IF EXISTS lol");
    db.execSQL(sqlCreate);
}}

Then i instance the class and try to add something to the database:

SqlLiteDataBase sql = new SqlLiteDataBase(this, "DBlol", null, 1);
    SQLiteDatabase db = sql.getWritableDatabase();
    if(db != null)
    {
        int code = 1;
        String name = "test";
        db.execSQL("INSERT INTO lol (code, name) " + "VALUES (" + code + ", '" + name +"')");
        db.close();
    }

But nothing is added! it is suppose to be a file in the cel phone at data/... but the folder is empty, i try to se with the DDMS what is happenning, but nothing is displayed, what is going on? Is this a permission problem? here are my permissions:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

Thanks for your time

2 Answers 2

1

I think you forgot a semicolon in your sqlCreate string.

String sqlCreate = "CREATE TABLE lol (code INTEGER, name TEXT);";
Sign up to request clarification or add additional context in comments.

3 Comments

added, and the logcat shows the next error: Failure 1 (table lol has no column named code) on 0x15c008 when preparing 'INSERT INTO lol (code, name) VALUES (1, 'test')'.
I think you mentioned translating the code from Spanish to English for your post. Did you translate it back to match the original column name? I only ask because when I test the the code the insert seems to work fine.
I created a small test project using the above code. When I run the app the name value is added each time it is run with no problems. It may be that when you updated the code the DB did not get upgraded properly. The original table probably still exists since the DB version hasn't changed and therefore onUpgrade() hasn't been called. You might try uninstalling the app from your test device or emulator which should remove the DB then do a fresh reinstall and try that. You could also change the DB version in your constructor from 1 to 2 which should cause onUpgrade() to be called.
0

try using this "+ name +" instead of this '" + name +"'. I think there is no need of ' for variable.
Or, try to use the following standard approach.

 public long createEntry(String code, String name) {
    ContentValues initialValues = new ContentValues();
    initialValues.put("code", code);
    initialValues.put("name", name);

    return db.insert("lol", null, initialValues);
}

for more help on DB related things I suggest This

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.