我尝试在我的应用程序中使用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()方法永远不会被调用?请原谅我如果我因为不解决这个问题一周而感到疯狂,我会滥用某些条款:(
我在我的代码中使用了这个: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 :(得分: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
祝你好运!