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

user3340336
  • 37
  • 2
  • 8

4 Answers4

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);
}
Looking Forward
  • 3,579
  • 8
  • 45
  • 65
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

Palejandro
  • 2,092
  • 5
  • 26
  • 38
  • 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? – user3340336 Feb 22 '14 at 11:25
  • Saved my days.. I was doing the same mistake - passing the ID but I think it is not required. Database automatically adds key. – atp9 Oct 11 '15 at 13:52
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?

Community
  • 1
  • 1
laalto
  • 150,114
  • 66
  • 286
  • 303
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