Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
package com.example.vistiorg;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class VisitorDatabase extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 2;
    private static final String DATABASE_NAME = "visitordb";
    private static final String DATABASE_TABLE = "visitortable";

    //Column names for database
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_PHONE = "phone";
    private static final String KEY_CONTACTPERSON = "contactperson";
    private static final String KEY_APTNO = "aptno";
    private static final String KEY_PURPOSE = "purpose";
    private static final String KEY_DATE = "date";
    private static final String KEY_TIME = "time";
    private Visitor visitor;


    VisitorDatabase(Context context) {
        super(context, DATABASE_NAME,null, DATABASE_VERSION);

    }



    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        //CREATE TABLE nametame(id INT PRIMARY KEY, name TEXT, phone INT, contactperson TEXT, aptno INT, purpose TEXT,date TEXT, time TEXT);
        String query= "CREATE TABLE "+DATABASE_TABLE+"("+
                       KEY_ID+" INT PRIMARY KEY,"+
                       KEY_NAME+" TEXT,"+
                       KEY_PHONE+" TEXT,"+
                       KEY_CONTACTPERSON+" TEXT,"+
                       KEY_APTNO+" TEXT,"+
                       KEY_PURPOSE+" TEXT,"+
                       KEY_DATE+" TEXT,"+
                       KEY_TIME+" TEXT"+")";

        sqLiteDatabase.execSQL(query);

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        if(i >= i1)
            return;
        sqLiteDatabase.execSQL(" DROP TABLE IF EXISTS " +DATABASE_TABLE);
        onCreate(sqLiteDatabase);

    }

    public long addVisitor(Visitor visitor) {

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues c= new ContentValues();

        c.put(KEY_NAME, visitor.getName());
        c.put(KEY_PHONE, visitor.getPhone());
        c.put(KEY_CONTACTPERSON, visitor.getContactperson());
        c.put(KEY_APTNO, visitor.getAptno());
        c.put(KEY_PURPOSE, visitor.getPurpose());
        c.put(KEY_DATE,visitor.getDate());
        c.put(KEY_TIME, visitor.getTime());

        //inserting data into db
        long ID= db.insert(DATABASE_TABLE, null,c);
        Log.d("Inserted", "ID ->"+ ID);
        return ID;


    }
}


What I have tried:

I have tried clearing the app's data, uninstalling the app. It hasn't worked for me. Please help.
Posted
Updated 30-Aug-20 21:36pm
v2
Comments
Richard MacCutchan 31-Aug-20 4:20am    
The message is telling you that all your visitor values are null.
David Crow 31-Aug-20 9:20am    
I can't improve upon Richard's answer, but I would like to add that I think _id is a required column. Just for grins, you might could try incrementing DATABASE_VERSION.

1 solution

I can't pick anything obvious, unless the app is still finding a copy of the database left on the device/workspace

One thing I'd say, is that (Golum voice) 'we hates SQL written like that' - its so error prone, and from a lot of personal experience with SQLite (not Android), it does funny things sometimes with column names and spacing... usually we/I would suggest parameterised queries etc, I don't know if you can in your Android framework - you can tidy the statement up a bit using this technique https://shyr.io/blog/android-create-tables-in-sqlite-beautifully[^] which would also make it less error-prone (ie, use a builder to build the SQL
 
Share this answer
 

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