无法使用自定义sqlite绑定创建数据库

时间:2019-07-15 11:38:18

标签: java android android-sqlite

我为android studio定制了内置Sqlite。 为了能够存储多边形坐标。

但是运行我的代码会产生错误

  

由以下原因导致:org.sqlite.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(代码14):无法打开数据库

尝试访问getReadableDatabase()时

public class MainActivity extends AppCompatActivity

    implements NavigationView.OnNavigationItemSelectedListener {

    private  TestOpenHelper mOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    System.loadLibrary("sqliteX");
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mOpenHelper = new TestOpenHelper(this);

    mOpenHelper.getReadableDatabase();
  

TestOpenHelper类

import  org.sqlite.database.sqlite.SQLiteDatabase;;
import org.sqlite.database.sqlite.SQLiteOpenHelper;

public class TestOpenHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "test.db";
public static final int DaATABASE_VERISION = 1;
public TestOpenHelper(Context context) {
    super(context, DATABASE_NAME, null, DaATABASE_VERISION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TestDatabaseContract.OwnerInfoEntry.SQL_CREATE_TABLE);
    db.execSQL(TestDatabaseContract.PropertyInfoEntry.SQL_CREATE_TABLE);


    DatabaseDataWorker worker = new DatabaseDataWorker(db);
    worker.insertOwnerInfo();
    worker.insertPropertyInfo();


}
  

DatabaseCoontract类

import android.provider.BaseColumns;


public final class TestDatabaseContract {
private TestDatabaseContract() {}

public static  final class  PropertyInfoEntry {
    public static final String TABLE_NAME = "property_info";
    public static  final String COLUMN_PROPERTY_ID = "property_id";

    public  static final String SQL_CREATE_TABLE =
            "CREATE TABLE " + TABLE_NAME + " USING geopoly(" + COLUMN_PROPERTY_ID  +  ")";
}
public static  final class OwnerInfoEntry implements  BaseColumns{
    public static final String TABLE_NAME = "owner_info";
    public static  final String COLUMN_OWNER_ID = "id";
    public static  final String COLUMN_OWNER_NAME = "name";
    public static  final String COLUMN_OWNER_LOCATION = "location";
    public static  final String COLUMN_PROPERTY_ID = "property_id";

    public  static final String SQL_CREATE_TABLE =
            "CREATE TABLE " + TABLE_NAME + " (" +
                    _ID + " INTEGER PRIMARY KEY, " +
                    COLUMN_OWNER_ID + " INTEGER UNIQUE NOT NULL, " +
                    COLUMN_OWNER_NAME + " TEXT NOT NULL, " +
                    COLUMN_OWNER_LOCATION + " TEXT NOT NULL, " +
                    COLUMN_PROPERTY_ID + " TEXT)";
}

}

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,并使用CL提供的解决方案here

解决了该问题