0

I have a problem using my database. Please check this:

// Database Name
private static final String DATABASE_NAME = "Poloha";

// Data table name
private static final String DATA = "Data";

// Data Table Columns names
private static final String KEY_Y= "Y";
private static final String KEY_X= "X";
private static final String KEY_ID = "_id";
private static final String KEY_NAZEV = "Nazev";
private static final String KEY_MAC = "MAC";

public void onCreate(SQLiteDatabase db) 
{
    String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS" 
            + DATA + "("
            + KEY_ID + " INTEGER PRIMARY KEY autoincrement,"
            + KEY_NAZEV + " TEXT NOT NULL,"
            + KEY_MAC + " TEXT NOT NULL," 
            + KEY_X + " TEXT NOT NULL," 
            + KEY_Y + " TEXT NOT NULL);" ;
    db.execSQL(CREATE_CONTACTS_TABLE);
}

Now I want to add some values using:

db.pridejZaznam(new Data(1,1,1,"NAZEV_1","MAC_1"));
db.pridejZaznam(new Data(2,2,2,"NAZEV_2","MAC_2"));
db.pridejZaznam(new Data(3,3,3,"NAZEV_3","MAC_3"));
db.pridejZaznam(new Data(4,4,4,"NAZEV_4","MAC_4"));

Problem is, LogCat is showing me this error:

02-22 10:33:26.336: E/SQLiteLog(813): (1) table Data has no column named Y
02-22 10:33:26.375: E/SQLiteDatabase(813): Error inserting Y=1 MAC=MAC_1 Nazev=NAZEV_1 _id=1 X=1
02-22 10:33:26.375: E/SQLiteDatabase(813): android.database.sqlite.SQLiteException: table Data has no column named Y (code 1): , while compiling: INSERT INTO Data(Y,MAC,Nazev,_id,X) VALUES (?,?,?,?,?)

Any idea what should I do? I´ve check similar errors and found only a solution with a missing SPACE while creating table but if I´m not blind, I have it without anything missing. Thanks all :)

4 Answers 4

5
public void onCreate(SQLiteDatabase db) 
{
    String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS" 
            + DATA + "("
            + KEY_ID + " INTEGER PRIMARY KEY," // and auto increment will be handled with                            primary key
            + KEY_NAZEV + " TEXT NOT NULL,"
            + KEY_MAC + " TEXT NOT NULL," 
            + KEY_X + " TEXT NOT NULL," 
            + KEY_Y + " TEXT NOT NULL);";
    db.execSQL(CREATE_CONTACTS_TABLE);
}
Sign up to request clarification or add additional context in comments.

Comments

1

you dont need to add KEY_ID, database will add it automatically, remove first value from your insert query :

db.pridejZaznam(new Data(1,1,"NAZEV_1","MAC_1"));

and so on..

and use english names for methods and variables for better underdstanding

2 Comments

Thanks, problem is my database is defined this way: public Data(long id, String nazev, String MAC,int x,int y) after few changes and it wants all 5 variables to be inserted. How can I force it to add ID automaticaly?
Saved my days.. I was doing the same mistake - passing the ID but I think it is not required. Database automatically adds key.
1

You need space between the keyword EXISTS and the table name here:

String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS" 
        + DATA + "("

change to:

String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS " 
        + DATA + "("

After editing the table schema, uninstall your app so the onCreate() gets run again. See also: When is SQLiteOpenHelper onCreate() / onUpgrade() run?

Comments

0

Just remove the app and install it again.

The MYSQLite table is stored in the local device and it does not append the tables

Comments

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.