表不是在Android应用程序中使用sqlite创建的

时间:2014-12-11 21:18:03

标签: java android android-sqlite android-json

我尝试在数据库中创建一个表。我正在使用这个框架:http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/

我编写了一些代码。在下面的代码中,创建了一个表login(如果我错了,请纠正我)。但我无法在数据库中看到任何名为login的表。

以下是完整的代码:

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

     //   Cursor cursor = db.rawQuery(selectQuery, null);
     //   System.out.print("Count"+cursor.getCount());
/*
        SQLiteDatabase db = this.getWritableDatabase();
        db.rawQuery("UPDATE login SET total_usage = 132 WHERE email = 'sampleMail'", null);
        db.rawQuery("UPDATE login SET total_usage = 132 WHERE email = 'sampleMail'", null);

        db.setTransactionSuccessful();
        db.close(); */
     //   db.rawQuery("UPDATE "+ TABLE_LOGIN + " SET "+ KEY_TOTAL_USAGE +" = " + total_usage + " WHERE " + KEY_EMAIL + " = '" + email + "'", null);
     //   db.rawQuery("UPDATE "+ TABLE_LOGIN + " SET "+ KEY_TOTAL_USAGE +" = " + total_usage + " WHERE " + KEY_EMAIL + " = '" + email + "'", null);

    }

    /**
     * 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();
    }

}

上面的代码(见下文)创建了表登录,对吗?

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

所以我的问题是:为什么没有名为login的表?还是我误解了什么? 如果我做错了什么,有人可以帮我解决吗?

应用程序正常运行(没有崩溃等),logcat没有输出

提前致谢

0 个答案:

没有答案