繁体   English   中英

SQLite数据库连接问题

[英]SQLite database connection issue

嗨,我试图与SQLlite数据库建立连接。 代码看起来不错,但未创建数据库表。应用程序运行良好,但未创建数据库。 有什么建议么?? 我的代码:

数据库管理器

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


}

您在那里的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 这样,如果您添加了一个表,其他人将不会在创建新表时抛出错误或被覆盖。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM