sqLite Android ..遗憾的是,应用已停止

时间:2015-06-11 06:04:58

标签: android database sqlite

我是Android编程新手。我正在尝试在android中创建sqLite数据库这是我的代码

sqLitee.java
package com.example.sqlite;

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

public class sqLitee extends SQLiteOpenHelper 
{

    public static final String DB_NAME="student.db";
    public static final String TABLE_NAME="student";
    public static final String COL_1="ID";
    public static final String COL_2="FIRST_NAME";
    public static final String COL_3="LAST_NAME";
    public static final String COL_4="MARKS";

    public sqLitee(Context context) 
    {
        super(context, DB_NAME,null, 1);
        // TODO Auto-generated constructor stub
        SQLiteDatabase db=this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTO INCREMENT, FIRST_NAME TEXT, LAST_NAME TEXT, MARKS INTEGER)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXIST " + TABLE_NAME);
        onCreate(db);

    }


}

MainActivity.java

package com.example.sqlite;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends Activity {

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

    }


}

当我尝试在Android虚拟设备上运行它时,不幸的是,sqLite已停止.. 感谢

2 个答案:

答案 0 :(得分:0)

在sqllite构造函数中删除此行:

SQLiteDatabase db=this.getWritableDatabase();

您尝试获取当前正在初始化的数据库。

答案 1 :(得分:0)

CREATE TABLE SQL中存在语法错误。 AUTO INCREMENT应为AUTOINCREMENT

与往常一样,首先要看logcat中的异常堆栈跟踪,并了解失败的原因和位置。