0

I'm having troubles triying to insert into a second table that i'm trying to call. My first table it's supposed to manage info about the user, and it's created when the user log in. My second table is created later. I'm not sure if the two tables have to be created at the same time.

public class SQLiteHandler extends SQLiteOpenHelper {

private static final String TAG = SQLiteHandler.class.getSimpleName();

//All Static variables
//Database Version
private static final int DATABASE_VERSION = 1;

//Database Name
private static final String DATABASE_NAME = "android_api";

//Login table name
private  static final String TABLE_USER = "user";

//Docs table name
private  static final String TABLE_DOCS = "docs";

//Login TAble Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_EMAIL = "email";
private static final String KEY_UID = "uid";
private static final String KEY_CREATED_AT = "created_at";

//Documentos Table
private static final String KEY_NAMEDOC = "name";
private static final String KEY_IMAGE  = "image_doc";

private static final String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USER + "("
        + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
        + KEY_EMAIL + " TEXT UNIQUE," + KEY_UID + " TEXT,"
        + KEY_CREATED_AT + " TEXT" + ");";

private static final String CREATE_DOCS_TABLE = "CREATE TABLE " + TABLE_DOCS + " ( "
        + KEY_NAMEDOC + " TEXT,"
        + KEY_IMAGE + " TEXT" + ");";

public SQLiteHandler (Context context){
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(CREATE_LOGIN_TABLE);
    db.execSQL(CREATE_DOCS_TABLE);

    Log.d(TAG, "Database created");
}

//Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
    //Drop older table if existed
    db.execSQL("DROP TABLE IF EXIST " + TABLE_USER);
    db.execSQL("DROP TABLE IF EXIST " + TABLE_DOCS);
    //Create tables again
    onCreate(db);
}

/**
 * Storing user details in database
 */
public void addUser(String name, String email, String uid, String created_at){
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, name);
    values.put(KEY_EMAIL, email);
    values.put(KEY_UID, uid);
    values.put(KEY_CREATED_AT, created_at);

    long id = db.insert(TABLE_USER, null, values);
    db.close(); //Closing database connection

    Log.d(TAG, "New user inserted into sqlite" +id);
}

/**
 * Guardar los archivos de ETAPA 1
 */
 public void addDocument(String name, String name2){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
     values.put(KEY_NAMEDOC, name);
     values.put(KEY_IMAGE, name2);
    db.insert(TABLE_DOCS, null, values);
    db.close();
    Log.d(TAG, "Se inserto el documento satisfactoriamente");
 }

}

When i try to Add a document

db.addDocument("Documento", "test");

Send the error android.database.sqlite.SQLiteException: no such table: docs (code 1): , while compiling: INSERT INTO docs(name,image_doc) VALUES (?,?)

The users table is created first and the second table it's created later...

JosCarrillo
  • 85
  • 1
  • 14
  • try unistalling and then installing the application – Shubham AgaRwal Aug 01 '17 at 00:28
  • You have 3 simple options a) re-install the app b) clear the App's data or c) increase the version number. All 3 will result in all the data being lost. The most likely reason why the error, is that `onCreate` is only automatically run when the database file itself does not exist, so if the database structure is changed after the original creation, then the change will not be applied by/via `onCreate`. increasing the version number causes `onUpgrade` to run, which invokes `onCreate` after dropping the tables. – MikeT Aug 01 '17 at 01:15
  • Possible duplicate of [When is SQLiteOpenHelper onCreate() / onUpgrade() run?](https://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run) – Selvin Aug 01 '17 at 02:06
  • Thanks you guys, its working now (:: – JosCarrillo Aug 01 '17 at 03:50

0 Answers0