2014-12-13 53 views
1

我嘗試在應用程序中使用sqlite創建表。在Android應用程序中未使用SQLite創建表

這裏是我的代碼:

public class DatabaseHandler extends SQLiteOpenHelper { 

    // 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_LOGIN = "login"; 

    // 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"; 
    private static final String KEY_TOTAL_USAGE = "total_usage"; 


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

    // Creating Tables 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "(" 
       + KEY_ID + " INTEGER PRIMARY KEY," 
       + KEY_NAME + " TEXT," 
       + KEY_EMAIL + " TEXT UNIQUE," 
       + KEY_UID + " TEXT," 
       + KEY_CREATED_AT + " TEXT," 
       + KEY_TOTAL_USAGE + " TEXT);"; 
     db.execSQL(CREATE_LOGIN_TABLE); 
    } 

    // Upgrading database 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // Drop older table if existed 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN); 

     // 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); // Name 
     values.put(KEY_EMAIL, email); // Email 
     values.put(KEY_UID, uid); // Email 
     values.put(KEY_CREATED_AT, created_at); // Created At 
    // values.put(KEY_TOTAL_USAGE, total_usage); // Total_usage 

     // Inserting Row 
     db.insert(TABLE_LOGIN, null, values); 

     db.close(); // Closing database connection 
    } 

    /** 
    * Storing user details in database 
    * */ 
    public void updateUser(String email, String total_usage) { 
     String selectQuery = "UPDATE login SET total_usage = 132 WHERE email = 'sampleMail'"; 

     SQLiteDatabase db = this.getWritableDatabase(); 

     db.execSQL(selectQuery); 

     db.setTransactionSuccessful(); 
     db.close(); 
    } 

    /** 
    * Getting user data from database 
    * */ 
    public HashMap<String, String> getUserDetails(){ 
     HashMap<String,String> user = new HashMap<String,String>(); 
     String selectQuery = "SELECT * FROM " + TABLE_LOGIN; 

     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 
     // Move to first row 
     cursor.moveToFirst(); 
     if(cursor.getCount() > 0){ 
      user.put("name", cursor.getString(1)); 
      user.put("email", cursor.getString(2)); 
      user.put("uid", cursor.getString(3)); 
      user.put("created_at", cursor.getString(4)); 
     } 
     cursor.close(); 
     db.close(); 
     // return user 
     return user; 
    } 

    /** 
    * Getting user login status 
    * return true if rows are there in table 
    * */ 
    public int getRowCount() { 
     String countQuery = "SELECT * FROM " + TABLE_LOGIN; 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(countQuery, null); 
     int rowCount = cursor.getCount(); 
     db.close(); 
     cursor.close(); 

     // return row count 
     return rowCount; 
    } 

    /** 
    * Re crate database 
    * Delete all tables and create them again 
    * */ 
    public void resetTables(){ 
     SQLiteDatabase db = this.getWritableDatabase(); 
     // Delete All Rows 
     db.delete(TABLE_LOGIN, null, null); 
     db.close(); 
    } 

} 

我的問題是:爲什麼表login從未產生的?難道我做錯了什麼?它不可能很難解決這樣的問題。我遇到很多困難,使用Sqlite。如果有人能幫助我,那將會很好。即時通訊使用phpmyadmin的數據庫

應用程序本身沒有給出誤差修改,logcat的是空

我刪除,並運行應用程序十億次,但沒有奏效

也許這信息是有用的:當我在終端中鍵入adb,則無法識別。

爲什麼onCreate()方法永遠不會調用?原諒我,如果我濫用的一些術語,因爲我通過:(

我用這個在我的代碼不解決這個問題1周變得瘋狂:http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/


下面的代碼沒有工作:

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

回答

0

,直到調用SQLiteOpenHelper類的方法getWritableDatabase()或getReadableDatabase(),數據庫將不會被創建。

你可以做什麼存儲變量場 SQLiteDatabase db; 然後在構造函數調用db = db.getWriteableDatabase()/readable();

我認爲這將解決您的問題:)

編輯:試試這個:

private SQLiteDatabase db; 

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

public void open(){ 
    db = getWritableDatabase(); 
} 

然後做這樣的事情:

DatabaseHandler dh = new DatabaseHandler(); 
dh.open() 

如果仍然不起作用,請嘗試 https://www.google.se/?gws_rd=ssl#q=android+sqlite+oncreate+not+called

祝你好運!

+0

如果我錯了,請糾正我,但我已經在updateUser()函數中這樣做了。 – Simple 2014-12-13 01:02:25

+0

你嘗試在構造函數中調用getWriteable嗎?在新設備上首次調用getWriteable/readable時,應調用onCreate,如果更改了db版本號,則調用onUpgrade。所以,即使您認爲您將應用程序從您的ide重新上傳到您的手機,onCreate也只會在您第一次上傳應用程序時被調用一次。 – Jakkra 2014-12-13 01:06:48

+0

請問你能寫代碼嗎(或者我必須把代碼放在哪裏)?通常我不直接詢問代碼,但這個問題浪費了我一週的時間 – Simple 2014-12-13 01:14:25

相關問題