android - SQLiteDatabase如何使用两个表?

时间:2017-07-31 23:59:25

标签: android android-sqlite

我遇到麻烦,试图插入我想要打电话的第二张桌子。我的第一个表应该管理有关用户的信息,并在用户登录时创建。我的第二个表是稍后创建的。我不确定是否必须同时创建这两个表。

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");
 }

}

当我尝试添加文档时

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

发送错误 android.database.sqlite.SQLiteException: no such table: docs (code 1): , while compiling: INSERT INTO docs(name,image_doc) VALUES (?,?)

首先创建users表,然后创建第二个表...

0 个答案:

没有答案