2016-02-25 21 views
-1

你好,我嘗試開發一個基於SQLiteDatabase的應用程序。 我開發了創建數據庫的簡單代碼,但它在AVD中不工作意味着它的應用程序崩潰。請大家幫我解決。SQLiteOpenHelper問題

我發佈我的代碼和錯誤。這是非常可觀的。

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) 
+0

它在Android設備監視器中創建數據庫 – Bhaumik

+2

您發佈的錯誤與代碼無關。您需要發佈代碼導​​致的錯誤。 – Kuffs

+0

我更新了一些錯誤 – Bhaumik

回答

1

你需要SQL關鍵字和標識符如表名之間的空白。

變化

"create table" + TABLE_NAME 

"create table " + TABLE_NAME 
+0

它不起作用 – Bhaumik

+0

這是您發佈的代碼中的錯誤。如果沒有更具體的問題,我無法進一步幫助你。 – laalto

+0

「(ID INTEGER PRIMARY KEY AUTOINCREMENT)」)這裏可能有錯誤嗎? – Bhaumik

0

對不起傢伙浪費你的時間我犯了一個愚蠢的錯誤。

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

代替這種 db.execSQL( 「創建表」 + TABLE_NAME + 「(ID INTEGER PRIMARY KEY AUTOINCREMENT)」);

空間應用程序崩潰。 感謝您的一切幫助。