SQLiteOpenHelper问题

时间:2016-02-25 18:54:07

标签: android database sqlite sqliteopenhelper

您好我尝试基于SQLiteDatabase开发一个应用程序。 我开发了简单的代码来创建数据库但它在AVD中不起作用意味着它崩溃了应用程序。请大家帮帮我。

我发布了我的代码和错误。这非常值得赞赏。

DatabaseHelper.java

package com.example.bhaumik.sqlitedatabasetest;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by Bhaumik on 25-02-2016.
 */
public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "Student.db";
    public static final String TABLE_NAME = "student";
    public static final String COLUMN_1 = "ID";
    public static final String COLUMN_2 = "NAME";
    public static final String COLUMN_3 = "SURNAME";



    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
        SQLiteDatabase db = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table" + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT)");
        //db.execSQL("create table " + TABLE_NAME +
            //    " (ID INTEGER AUTOINCREMENT , PRODUCT_NAME PRIMARY KEY TEXT , PRODUCT_SIZE INTEGER , PRODUCT_RUPPESS INTEGER , BUY_NUMBER INTEGER)");



    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}

MainActivity.java

package com.example.bhaumik.sqlitedatabasetest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    DatabaseHelper mydb ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mydb = new DatabaseHelper(this);

    }
}

同时发布错误

02-26 00:06:43.030 1898-1898/? E/NetworkScheduler.SchedulerReceiver: Invalid parameter app
02-26 00:06:43.030 1898-1898/? E/NetworkScheduler.SchedulerReceiver: Invalid package name : Perhaps you didn't include a PendingIntent in the extras?


  02-26 07:30:49.886 952-952/? E/lowmemorykiller: Kernel does not support memory pressure events or in-kernel low memory killer
 02-26 07:30:50.575 972-972/? E/perfprofd: unable to open configuration file /data/data/com.google.android.gms/files/perfprofd.conf
 02-26 07:30:51.750 954-954/? E/libEGL: load_driver(/system/lib/egl/libGLES_emulation.so): dlopen failed: library "/system/lib/egl/libGLES_emulation.so" not found
02-26 07:30:54.291 965-965/? E/CameraService: setUpVendorTags: Vendor tag operations not fully defined. Ignoring definitions.
  02-26 07:30:55.845 954-989/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
  02-26 07:30:55.848 1135-1236/? E/libEGL: load_driver(/system/lib/egl/libGLES_emulation.so): dlopen failed: library "/system/lib/egl/libGLES_emulation.so" not found
   02-26 07:30:56.876 961-961/? E/Netd: cannot find interface dummy0
  02-26 07:30:59.643 965-965/? E/SoundTriggerHwService: couldn't load sound trigger module sound_trigger.primary (No such file or directory)
 02-26 07:30:59.643 965-965/? E/RadioService: couldn't load radio module radio.primary (No such file or directory)
    02-26 07:30:59.644 965-1293/? E/AudioFlinger: no wake lock to update!
  02-26 07:31:04.113 970-970/? E/memtrack: Couldn't load memtrack module (No such file or directory)
    02-26 07:31:04.113 970-970/? E/android.os.Debug: failed to load memtrack module: -2
  02-26 07:31:07.698 970-970/? E/Minikin: addFont failed to create font /system/fonts/NanumGothic.ttf
    02-26 07:31:07.698 970-970/? E/Minikin: addFont failed to create font /system/fonts/DroidSansFallback.ttf
    02-26 07:31:07.698 970-970/? E/Minikin: addFont failed to create font /system/fonts/MTLmr3m.ttf
 02-26 07:31:13.573 966-966/? E/installd: eof
    02-26 07:31:13.573 966-966/? E/installd: failed to read size
 02-26 07:31:16.842 1305-1305/? E/art: DexFile_getDexOptNeeded file '/system/framework/org.apache.http.legacy.jar' does not exist
  02-26 07:31:26.749 1305-1305/? E/ConsumerIrService: Can't open consumer IR HW Module, error: -2
02-26 07:31:27.624 1305-1342/? E/EventHub: could not get driver version for /dev/input/mice, Not a typewriter
 02-26 07:31:27.792 954-954/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
  02-26 07:31:33.093 1305-1305/? E/LockSettingsStorage: Cannot read file java.io.FileNotFoundException: /data/system/gatekeeper.password.key: open failed: ENOENT (No such file or directory)
    02-26 07:31:33.093 1305-1305/? E/LockSettingsStorage: Cannot read file java.io.FileNotFoundException: /data/system/password.key: open failed: ENOENT (No such file or directory)
    02-26 07:31:33.093 1305-1305/? E/LockSettingsStorage: Cannot read file java.io.FileNotFoundException: /data/system/gatekeeper.pattern.key: open failed: ENOENT (No such file or directory)
    02-26 07:31:33.093 1305-1305/? E/LockSettingsStorage: Cannot read file java.io.FileNotFoundException: /data/system/gatekeeper.gesture.key: open failed: ENOENT (No such file or directory)
    02-26 07:31:33.093 1305-1305/? E/LockSettingsStorage: Cannot read file java.io.FileNotFoundException: /data/system/gesture.key: open failed: ENOENT (No such file or directory)

2 个答案:

答案 0 :(得分:1)

SQL关键字和表名等标识符之间需要空格。

更改

"create table" + TABLE_NAME

"create table " + TABLE_NAME

答案 1 :(得分:0)

抱歉,浪费你的时间我犯了一个愚蠢的错误。

db.execSQL("create table" + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT)");

而不是这个     db.execSQL(“create table”+ TABLE_NAME +“(ID INTEGER PRIMARY KEY AUTOINCREMENT)”);

空间使应用程序崩溃。 谢谢你的帮助。