您好我正在尝试与SQLlite数据库建立连接。代码看起来很好但是没有创建数据库表.App运行良好但是没有创建数据库。有什么建议??我的代码:
DatabaseManager.java
package com.step2rock.www.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
import com.step2rock.www.model.User;
/**
* Created by Sushimz on 5/15/2016.
*/
public class DatabaseManager extends SQLiteOpenHelper {
Context context;
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "photography";
// Users table name
private static final String TABLE_USERS = "users";
//blog table name
private static final String TABLE_BLOG = "blog";
// Users Table Columns names
private static final String KEY_USER_ID = "id";
private static final String KEY_USER_first_NAME = "f_name";
private static final String KEY_USER_last_NAME = "l_name";
private static final String KEY_USER_PASS = "password";
private static final String KEY_USER_PIC = "profilepic";
private static final String KEY_USER_Address = "address";
private static final String KEY_USER_Exp = "exp_level";
private static final String KEY_USER_link = "link";
private static final String KEY_USER_TYPE = "type";
// Blog Table Columns names
private static final String KEY_Blog_ID = "id";
private static final String KEY_Blog_Title = "blog_tilte";
private static final String KEY_Blog_Desc = "blog_desc";
private static final String KEY_Blog_Image = "blog_image";
private static final String KEY_Blog_Link = "blog_link";
public DatabaseManager(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d("users",TABLE_USERS);
Log.d("blog",TABLE_BLOG);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_USERS_TABLE = "CREATE TABLE " + TABLE_USERS
+ "("
+ KEY_USER_ID + " INTEGER PRIMARY KEY,"
+ KEY_USER_first_NAME + " TEXT,"
+ KEY_USER_last_NAME + " TEXT,"
+ KEY_USER_PASS + " TEXT,"
+ KEY_USER_PIC + " TEXT,"
+ KEY_USER_Address + " TEXT,"
+ KEY_USER_Exp + " TEXT,"
+ KEY_USER_link + " TEXT,"
+ KEY_USER_TYPE + " TEXT,"
+ ")";
db.execSQL(CREATE_USERS_TABLE);
String CREATE_BLOG_TABLE = "CREATE TABLE " + TABLE_BLOG
+ "("
+ KEY_Blog_ID + " INTEGER PRIMARY KEY,"
+ KEY_Blog_Title + " TEXT,"
+ KEY_Blog_Desc + " TEXT,"
+ KEY_Blog_Image + " TEXT,"
+ KEY_Blog_Link + " TEXT ,"
+ " FOREIGN KEY(" + KEY_USER_ID + ") REFERENCES Users(id) ON DELETE CASCADE "
+ ")";
db.execSQL(CREATE_BLOG_TABLE);
Toast.makeText(this.context, "AAA", Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_BLOG);
// Create tables again
onCreate(db);
}
public void resetDB() {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_BLOG);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new USER
public int addUser(User user) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_USER_ID, user.get_user_id());
values.put(KEY_USER_first_NAME, user.get_first_name());
values.put(KEY_USER_last_NAME, user.get_last_name());
values.put(KEY_USER_PASS, user.get_user_pass());
values.put(KEY_USER_PIC, user.get_user_pic());
values.put(KEY_USER_Address, user.get_user_address());
values.put(KEY_USER_Exp, user.get_user_exp());
values.put(KEY_USER_link, user.get_user_link());
values.put(KEY_USER_TYPE, user.get_user_type());
// Inserting Row
int last_id = (int) db.insert(TABLE_USERS, null, values);
db.close(); // Closing database connection
return last_id;
}
// Updating single User
public int updateUser(User user) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_USER_ID, user.get_user_id());
values.put(KEY_USER_first_NAME, user.get_first_name());
values.put(KEY_USER_last_NAME, user.get_last_name());
values.put(KEY_USER_PASS, user.get_user_pass());
values.put(KEY_USER_PIC, user.get_user_pic());
values.put(KEY_USER_Address, user.get_user_address());
values.put(KEY_USER_Exp, user.get_user_exp());
values.put(KEY_USER_link, user.get_user_link());
values.put(KEY_USER_TYPE, user.get_user_type());
// updating row
return db.update(TABLE_USERS, values, KEY_USER_ID + " = ?",
new String[]{String.valueOf(user.get_user_id())});
}
}
RegistrationActivity.java
package com.step2rock.www.crudproject;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.step2rock.www.database.DatabaseManager;
/**
* Created by Sushimz on 5/15/2016.
*/
public class RegistrationActivity extends AppCompatActivity {
Button btnSaveRecord;
DatabaseManager databaseManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration_activity);
btnSaveRecord = (Button) findViewById(R.id.btnSaveRecord);
databaseManager = new DatabaseManager(RegistrationActivity.this);
btnSaveRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Toast toast = Toast.makeText(RegistrationActivity.this,"welcome",Toast.LENGTH_SHORT);
toast.show();
}
});
}
}
答案 0 :(得分:0)
你在那里SQLiteOpenHelper
看起来很不错。
令人困惑onCreate(...)
永远不会被调用,因为它与Activity.onCreate(...)
没有相同的行为。
来自文档:
首次创建数据库时调用。这是表的创建和表的初始填充的地方。
据我所知,您的代码永远不会创建数据库(除非您在别处执行此操作)。要创建它,您需要尝试在数据库上执行某些操作。例如,尝试在按钮上单击添加用户。这将调用getWriteableDdatabase()
并应调用onCreate(...)
:
btnSaveRecord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
int id = databaseManager.addUser(new User(...));
Toast toast = Toast.makeText(RegistrationActivity.this,"Welcome User #" + id,Toast.LENGTH_SHORT);
toast.show();
}
});
检查这是否会调用您的onCreate(...)
方法。
我还建议您将IF NOT EXISTS
添加到您的createTable字符串中,如下所示:"CREATE TABLE IF NOT EXISTS " + TABLE_USERS
。这样,如果您添加了一个表,其他表将不会抛出错误或在创建新表时被覆盖。