android - SQL 不创建表(或先创建数据库?)

标签 android sql android-sqlite

在 logcat 中我有这个错误:

12-05 19:20:26.696: E/SQLiteDatabase(1415): Error inserting lon=5.25 lat=7.52 name=null
12-05 19:20:26.696: E/SQLiteDatabase(1415): android.database.sqlite.SQLiteException: no such table: position (code 1): , while compiling: INSERT INTO position(lon,lat,name) VALUES (?,?,?)

我尝试在名为 position 的表中插入 3 个值:name、lat、lon。在代码中,我没有任何错误,应用程序也没有崩溃。我已将插入值的事件附加到带有 onClickListener 的按钮(当用户按下按钮时,插入值)。

数据库创建代码:

public class listagpsdb {

SQLiteDatabase mDb;
DbHelper mDbHelper;
Context mContext;
private static final String DB_NAME="listagps";
private static final int DB_VERSION=1;

public listagpsdb(Context ctx){
        mContext=ctx;
        mDbHelper=new DbHelper(ctx, DB_NAME, null, DB_VERSION);
}

public void open(){  //il database su cui agiamo è leggibile/scrivibile
        mDb=mDbHelper.getWritableDatabase();

}

public void close(){ 
        mDb.close();
}




public void insertPosition(String name,double lat, double lon){ 
        ContentValues cv=new ContentValues();
        cv.put(PositionsMetaData.POSITIONS_NAME_KEY, name);
        cv.put(PositionsMetaData.POSITIONS_LATITUDE_KEY, lat);
        cv.put(PositionsMetaData.POSITIONS_LONGITUDE_KEY, lon);
        mDb.insert(PositionsMetaData.POSITIONS_TABLE, null, cv);
}

public Cursor fetchProducts(){ 
        return mDb.query(PositionsMetaData.POSITIONS_TABLE, null,null,null,null,null,null);               
}

static class PositionsMetaData {  
        static final String POSITIONS_TABLE = "position";
        static final String ID = "_id";
        static final String POSITIONS_NAME_KEY = "name";
        static final String POSITIONS_LATITUDE_KEY = "lat";
        static final String POSITIONS_LONGITUDE_KEY = "lon";
}

private static final String POSITIONS_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS"  
                + PositionsMetaData.POSITIONS_TABLE + " (" 
                + PositionsMetaData.ID+ " integer primary key autoincrement, "
                + PositionsMetaData.POSITIONS_NAME_KEY + " text, "
                + PositionsMetaData.POSITIONS_LATITUDE_KEY + " double not null);"
                + PositionsMetaData.POSITIONS_LONGITUDE_KEY + "double not null);";

private class DbHelper extends SQLiteOpenHelper { 

        public DbHelper(Context context, String name, CursorFactory factory,int version) {
                super(context, name, factory, version);
        }

        @Override
        public void onCreate(SQLiteDatabase _db) { 
                _db.execSQL(POSITIONS_TABLE_CREATE);
        }

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


        }

}

}

在您看到的代码中,我只想在创建数据库时创建表。

这是 Activity 类的代码:

@Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_navi);



                Button SalvaLocation = (Button) findViewById(R.id.SalvaLocation);
                final listagpsdb db = new listagpsdb(getApplicationContext());
                SalvaLocation.setOnClickListener(new View.OnClickListener() {

                    public void onClick(View view) {



                    db.open(); 

                    String name = null;
                    Double lat = 7.52;
                    Double lon = 5.25;
                    db.insertPosition(name, lat, lon);

                    db.close();

                    }
                });

现在我不明白为什么没有创建数据库/表。有人有想法吗? 是否有一个技巧可以在 AVD 内存中查看数据库是否已创建?

感谢所有想帮助我的人!

最佳答案

PositionsMetaData.POSITIONS_LATITUDE_KEY 更新您的代码..

private static final String POSITIONS_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS"  
                + PositionsMetaData.POSITIONS_TABLE + " (" 
                + PositionsMetaData.ID+ " integer primary key autoincrement, "
                + PositionsMetaData.POSITIONS_NAME_KEY + " text, "
                + PositionsMetaData.POSITIONS_LATITUDE_KEY + " double not null,"
                + PositionsMetaData.POSITIONS_LONGITUDE_KEY + " double not null);";

关于android - SQL 不创建表(或先创建数据库?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13741385/

相关文章:

sql - 如何在 SQL 中找到重复的条目并删除最旧的条目?

java - session.list 上的 HSQLException。错误: Unexpected token "Entity name"

java - 无法将数据插入数据库 - android

java - 使用 Traces.txt 了解 ANR 错误

java - 图像在 ImageView 中不可见

android - cocos2dx无法实现暂停功能

sql - 忽略所有结果,即使一行具有不同的值

android - Android Market 中的应用程序图标损坏

android - 如何将 .db 文件从 assets 放到 data/data/packagename/android

android - 在 Android 上处理 SQLite 表和游标