我无法使用SQLite运行Android应用程序

时间:2015-11-17 04:52:57

标签: android android-sqlite

我试图在Android项目上创建一个数据库(SQLite,SQLiteOpenHelper),但当我运行我的应用程序时,应用程序说:

  

"抱歉,我的申请已经停止了#34;

为什么会这样?

我的逻辑数据库类:

public class DataBaseManager {

    public static final String TABLE_NAME = "Clientes";

    public static final String CN_ID = "_nuip";
    public static final String CN_NAME = "Name";
    public static final String CN_DATE = "Date";
    public static final String CN_CASH = "Cash";

    public static final String CREATE_TABLE = " create tabla" + TABLE_NAME + " ("
            + CN_ID + " integer primary key autoincrement , "
            + CN_NAME + " text not null,"
            + CN_DATE + " text not null,"
            + CN_CASH + " text);";
}

我的类扩展了SQLiteOpenHelper:

public class AdminSQLiteOpenHelper extends SQLiteOpenHelper {

    private static final String DB_NAME  = "clientes.sqlite" ;
    private static final int DB_SCHEME_VERSION = 1;

    public AdminSQLiteOpenHelper(Context context) {
         super(context,DB_NAME, null, DB_SCHEME_VERSION);
    }

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

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

    }
}

我的主要活动:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AdminSQLiteOpenHelper adm = new AdminSQLiteOpenHelper(this);
        SQLiteDatabase db2 = adm.getWritableDatabase();
    }
}

2 个答案:

答案 0 :(得分:0)

首先,在设置>经理应用程序>我的应用程序中删除应用程序的内存缓存和数据 然后,在您的查询中添加此行" IF NOT EXISTS" ,所以你的查询是;
public static final String CREATE_TABLE = " create tabla" + TABLE_NAME + " IF NOT EXISTS (" + CN_ID + " integer primary key autoincrement , " + CN_NAME + " text not null," + CN_DATE + " text not null," + CN_CASH + " text);";

答案 1 :(得分:0)

使用此代码更正您的代码。

public static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS" 
    + TABLE_NAME + " ("
    + CN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
    + CN_NAME + " TEXT NOT NULL, "
    + CN_DATE + " TEXT NOT NULL, "
    + CN_CASH + " TEXT);";
}