簡體   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