在Android中创建并连接到SQLite DB

时间:2013-02-03 17:45:21

标签: java android database sqlite

我正在尝试为我的Android应用程序创建一个SQLite Db,我尝试了很多在线教程,但没有一个能为我工作!我在下面发布了我的代码:

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 = "usersManager";

    // Contacts table name
    private static final String TABLE_CONTACTS = "users";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + ")";
        db.execSQL(CREATE_CONTACTS_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_CONTACTS);

        // Create tables again
        onCreate(db);
    }



import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;


public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DatabaseHandler db = new DatabaseHandler(this);

我无法在代码中看到错误,但是当我检查位置时,应该创建db,没有任何内容。

6 个答案:

答案 0 :(得分:4)

除了避免长串的串联字符串:

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = 
            "CREATE TABLE " + TABLE_CONTACTS + 
            "(" + 
                KEY_ID   + " INTEGER PRIMARY KEY," + 
                KEY_NAME + " TEXT" + //--last value has no comma--
            ")";

    db.execSQL(CREATE_CONTACTS_TABLE);
}

调用getWritableDataBase()打开数据库,如果它不存在则会创建。

答案 1 :(得分:3)

试试这个

public class DataBase extends SQLiteOpenHelper{


    static String createBDSQL = "CREATE TABLE Notas (id integer primary key autoincrement, title TEXT)";

    public DataBase(Context context, String name, CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(createBDSQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS Notas");
            db.execSQL(createBDSQL);
    }
}

在MainActivity.java中

DataBase notasdb = new DataBase(this, "DBSample.db", null, 1);
SQLiteDatabase db = notasdb.getWritableDatabase();

答案 2 :(得分:1)

SQLiteOpenHelper是懒惰的。在必要之前,它不会执行任何操作。因此,只有在尝试访问其中的数据(或插入新数据)时才会创建数据库。

答案 3 :(得分:0)

在创建要执行的查询时必须要小心..特别是使用空格和逗号。

更改此行

String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);

使用:

String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + " (" + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_NAME + " TEXT)";
        db.execSQL(CREATE_CONTACTS_TABLE);

答案 4 :(得分:0)

您确实没有创建数据库,只创建SQLiteOpenHelper的实例,但是对于创建或打开数据库,您必须使用getWritableDatabase()或getReadableDatabase()方法。

SQLiteDatabase db = new DataBaseHandler(this).getWritableDatabase();

答案 5 :(得分:0)

在您的主要活动中

SQLiteDatabase sql;
sql=db.getWritableDatabase(); (where db is DB Handler)