Click here to Skip to main content
15,877,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please guys need help. I can't insert data into database here is my logcat:
03-04 13:40:03.727 25642-25687/com.a00003454.tyreshopapp E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab12ca40
03-04 13:40:11.269 25642-25642/com.a00003454.tyreshopapp E/SQLiteLog: (1) near "table": syntax error
03-04 13:40:11.270 25642-25642/com.a00003454.tyreshopapp E/SQLiteDatabase: Error inserting Size=dvdvdvdvdv Cost=dvdv TyreBrand=dvdvdv
android.database.sqlite.SQLiteException: near "table": syntax error (code 1): , while compiling: INSERT INTO Tyre.table(Size,Cost,TyreBrand) VALUES (?,?,?)
   at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
   at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
   at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
   at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
   at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
   at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
   at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
   at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
   at com.a00003454.tyreshopapp.TyreDatabaseHelper.addTyre(TyreDatabaseHelper.java:43)
   at com.a00003454.tyreshopapp.AddTyreActivity.AddData(AddTyreActivity.java:56)
   at com.a00003454.tyreshopapp.AddTyreActivity$2.onClick(AddTyreActivity.java:45)
   at android.view.View.performClick(View.java:5198)
   at android.view.View$PerformClick.run(View.java:21147)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5417)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-04 13:40:11.358 25642-25687/com
And my codes:
Java
package com.a00003454.tyreshopapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by user on 04.03.2017.
 */
public class TyreDatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME="Tyre.db";
    public static final String TABLE_NAME="Tyre.table";
    public static final String COL_1="ID";
    public static final String COL_2="TyreBrand";
    public static final String COL_3="Size";
    public static final String COL_4="Cost";

    public TyreDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = ("CREATE TABLE Tyre (ID INTEGER PRIMARY KEY AUTOINCREMENT, TyreBrand TEXT Size INTEGER Cost INTEGER )");

        db.execSQL(sql);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(" DROP TABLE IF EXISTS Tyre");
        onCreate(db);
    }

    public boolean addTyre(String tyreBrand, String size, String cost ) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2, tyreBrand);
        contentValues.put(COL_3, size);
        contentValues.put(COL_4, cost);
        long result = db.insert(TABLE_NAME, null, contentValues);

        if (result == -1) {
            return false;
        } else {
           return true;
        }
    }

    public Cursor select() {
        SQLiteDatabase db =this.getWritableDatabase();
        Cursor data = db.rawQuery("SELECT * FROM Tyre",null);
        return data;

    }
}


What I have tried:

I think I have troubles with sql statements and tried to change then numerous times but no result
Posted
Updated 4-Mar-17 20:59pm
v2
Comments
Graeme_Grant 5-Mar-17 2:55am    
moved

First of all, your this table creation sql is not correct:
String sql = ("CREATE TABLE Tyre (ID INTEGER PRIMARY KEY AUTOINCREMENT, TyreBrand TEXT Size INTEGER Cost INTEGER )");
It should be:
String sql = "CREATE TABLE Tyre (ID INTEGER PRIMARY KEY AUTOINCREMENT, TyreBrand TEXT, Size INTEGER, Cost INTEGER)";
You would have encounter the error here, let alone inserting.
 
Share this answer
 
v3
Comments
Mir Usmanov 5-Mar-17 2:56am    
Yes Peter I changed the statement as you said but I faced the same problem with inserting I do not know what else can be the problem.=(
Graeme_Grant 5-Mar-17 3:15am    
The error message describes the problem well. Have a look at Solution 2.
Here is the error:
03-04 13:40:11.270 25642-25642/com.a00003454.tyreshopapp E/SQLiteDatabase: Error inserting Size=dvdvdvdvdv Cost=dvdv TyreBrand=dvdvdv
android.database.sqlite.SQLiteException: near "table": syntax error (code 1): , while compiling: INSERT INTO Tyre.table(Size,Cost,TyreBrand) VALUES (?,?,?)
My guess is that your test for inseting cost field as string "dvdv" instead of an integer value (cost should allow decimals like a currency type) would thow the error. Same with the size field. You should validate data before inserting into DB.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900