C
C
Cppper2018-02-23 19:22:24
Android
Cppper, 2018-02-23 19:22:24

Write error in SQLite?

Gives a write error to the database. I checked everything, but I could not find an error. Tested on a real device, without connecting through the studio. Therefore, there is no stack trace. Sorry. I am attaching the code. Please tell me what is the error
Helper class:

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



public class AvlsdeviceHelper extends SQLiteOpenHelper{

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "AveliskDb";
    public static final String TABLE_NAME = "Avls";

    public static final String KEY_ID = "_id";
    public static final String KEY_NAME = "name";
    public static final String KEY_MAC = "mac";
    public static final String KEY_PASS = "pass";

    public AvlsdeviceHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + KEY_ID
                + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_MAC + " TEXT," + KEY_PASS + " INTEGER" + ")");
    }

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

Write function, called from handler:
public void dataBase(){
        Avlsdevicehelper myhelper;
        myhelper = new AvlsdeviceHelper(this);
        ContentValues cv = new ContentValues();

        String name = kontroller.getText().toString();
        String mac = MacAdress;
        int pass = parol;

        SQLiteDatabase db = myhelper.getWritableDatabase();

        cv.put(AvlsdeviceHelper.KEY_NAME, name);
        cv.put(AvlsdeviceHelper.KEY_MAC, mac);
        cv.put(AvlsdeviceHelper.KEY_PASS, pass);

        long y = db.insert(AvlsdeviceHelper.TABLE_NAME, null, cv);
        if(y == -1){
            Toast.makeText(this, "Ошибка", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "Записано в строку " + y, Toast.LENGTH_SHORT).show();
        }
        myhelper.close();
        db.close();
        cv.clear();
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Neonoviiwolf, 2018-02-24
@Neonoviiwolf

spaces are missing

"(" + KEY_ID
" INTEGER PRIMARY KEY," + KEY_NAME
" TEXT," + KEY_PASS

maybe somewhere else

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question