SQLite数据库问题

时间:2014-03-22 11:57:12

标签: android database sqlite

我有veritabani.java,我在这个类中创建数据库。然后我在mainactivity.java中创建对象。但是,当我运行应用程序。该程序不移动database.java所以我不能创建数据库如何解决这个问题。

veritabani.java

    public class veritabani extends SQLiteOpenHelper {

    private static final String VERİTABANİ_ADİ="kayit";
    public veritabani(Context c)
    {
        super(c,VERİTABANİ_ADİ,null,2);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE kayit(ilacadi TEXT);");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXIST kayit");
        onCreate(db);   

    }
}

mainactivity.java

public class MainActivity extends Activity {

private veritabani v1;
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);

        v1=new veritabani(this);
}

v1 =新的veritabani(这个)不起作用。

2 个答案:

答案 0 :(得分:0)

我不确定你的意思是“程序不要移动database.java”,但我使用SQLiteOpenHelper时遇到了数据库问题。

实际上,SQLiteOpenHelper只调用一次“public void onCreate(SQLiteDatabase db)”,所以如果你在2次启动之间更改表,它就不会更新。

要解决此问题,您必须通过参数/ applications / [your_app] / erase data(或类似内容)手动删除数据库,然后重试。

答案 1 :(得分:0)

试试此代码

public class DBAdapter 
{
private static final String DATABASE_NAME = "GSDATA.db";

private static final int DATABASE_VERSION = 2;

private static final String CREATE_COVERPHOTO_TABLE = "CREATE TABLE IF NOT EXISTS     COVERPHOTO (path Text,date DATE);";


private final Context context;
public String group;
private DatabaseHelper DBHelper;
private static SQLiteDatabase db;


public DBAdapter(Context ctx)
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

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


        System.out.println("The Database is Created Here :");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // db.execSQL("DROP TABLE IF EXISTS calSimpleNote");
        onCreate(db);
    }
} // database helper class complete

// ---opens the database---
public DBAdapter open() throws SQLException {
    db = DBHelper.getWritableDatabase();
    return this;
}

// ---closes the database---
public void close() {
    DBHelper.close();
    db.close();
}

//并在主要活动中写下以下行

DBAdapter adapter = new DBAdapter(MainActivity.this);