1

in logcat i have this error:

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 (?,?,?)

I try to insert 3 value: name,lat,lon in the table named: position. In the code i don't have any error and the app don't crash. I have attached the event of the insert of the value to a button with a onClickListener (when the user press the button,insert the value).

The code of the database creation:

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) {


        }

}

}

In the code you see i want to create the table only when the database are created.

This the code of the activity class:

@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();

                    }
                });

Now i don't understand why the database/table are not created. Anyone have an idea? There's a trick to see in the AVD memory if the database are created or not?

Thank's to all want to help me!

0

2 Answers 2

2

update your code at 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);";
Sign up to request clarification or add additional context in comments.

8 Comments

Yes i have tryed now but don't change anything,same error. I don't understand,database hate me XD
Wait,now the logcat give me a line with and error. The line 42,is this line: mDb.insert(PositionsMetaData.POSITIONS_TABLE, null, cv);
@user1873069 : Do you need the PositionsMetaData class?? Else give directly "CREATE TABLE IF NOT EXISTS" + POSITIONS_TABLE + " (" ......
private static final String POSITIONS_TABLE_CREATE = "CREATE TABLE IF NOT EXIST" + POSITIONS_TABLE + "(" ID + "integer primary key autoincrement, " + POSITIONS_NAME_KEY + "text," + POSITIONS_LATITUDE_KEY + "double not null," + POSITIONS_LONGITUDE_KEY + "DOUBLE NOT NULL);" ; Something like this,without the PositionsMetaData Class?
I try now but this format is not correctly for Eclipse,i try to understand where is the error. The class PositionMetaData give the result for each variable in the POSITION_TABLE_CREATE code.
|
0

Yes you can see the Database if it have been created!..

Go to Window -> show view -> File Explorer,..

In that go to data -> data -> your package name-> and Database -> Your db

After that, you can use the SQL open db for opening yours Database and files..

3 Comments

Try with this simple example for yours database.. androidhive.info/2011/11/android-sqlite-database-tutorial
Ok,i have controlled in my phone (not in avd) and the db are present in the folder,now the problem is why don't create the table. I have read the tutoril and the only difference i have see is the command to create the table. I have wrote in the TABLE_CREATE = CREATE TABLE IF NOT EXIST you not,only CREATE TABLE
yes,i have tested in both: AVD and phone (both GS3). I have tried to chenge the sql command to create the table but nothing,the logcat give me the same error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.