Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4006414
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:30:43+00:00 2026-05-20T08:30:43+00:00

I’m a final year university IT Student and I’m having an issue trying to

  • 0

I’m a final year university IT Student and I’m having an issue trying to create a sqlite database within my android project. (I’m ok at programming but no expert, I’ve managed to find some code through various tutorials but I’m getting an error message when the code runs. It compiles fine and the rest of application works, just when it tries to put data into the database it causes an error.

Database Adapter code:

package com.fyp;

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

public class DBAdapter 
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_PFIRSTNAME = "pfirstname";
    public static final String KEY_PSURNAME = "psurname";
    public static final String KEY_PBUILDING= "pbuilding";
    public static final String KEY_PSTREET= "pstreet"; 
    public static final String KEY_PCITY = "pcity";
    public static final String KEY_PCOUNTY = "pcounty";
    public static final String KEY_PCOUNTRY = "pcountry";
    public static final String KEY_PPOSTCODE= "ppostcode";
    public static final String KEY_DFIRSTNAME = "dfirstname";
    public static final String KEY_DSURNAME = "dsurname";
    public static final String KEY_DBUILDING= "dbuilding";
    public static final String KEY_DSTREET= "dstreet";
    public static final String KEY_DCITY = "dcity";
    public static final String KEY_DCOUNTY = "dcounty";
    public static final String KEY_DCOUNTRY = "dcountry";
    public static final String KEY_DPOSTCODE= "dpostcode";
    public static final String KEY_LATITUDE = "latitude";
    public static final String KEY_LONGITUDE= "longitude"; 
    public static final String KEY_STATUS= "status"; 
    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "fypdb";
    private static final String DATABASE_TABLE = "packagetable";
    private static final int DATABASE_VERSION = 2;

    private static final String DATABASE_CREATE = "CREATE TABLE packagetable (_id integer primary key autoincrement, "
        + "pfirstname text not null, psurname text not null, pbuilding text not null, "
        + "pstreet text not null, pcity text not null, pcounty text not null, "
        + "pcountry text not null, ppostcode text not null, dfirstname text not null, "
        + "dsurname text not null, dbuilding text not null, dstreet text not null, "
        + "dcity text not null, dcounty text not null, dcountry text not null, "
        + "dpostcode text not null, latitude text not null, longitude text not null, "
        + "status text not null);";

    private final Context context; 

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
                              int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                  + " to "
                  + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS packagetable");
            onCreate(db);
        }
    }
  //---opens the database---
    public DBAdapter open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---reads the database---
    public DBAdapter read() throws SQLException 
    {
        db = DBHelper.getReadableDatabase();
        return this;
    }


    //---closes the database---    
    public void close() 
    {
        DBHelper.close();
    }

    //---insert a title into the database---
    public long insertPackage(String pfirstname, String psurname, 
            String pbuilding, String pstreet, String pcity, String pcounty, 
            String pcountry,  String ppostcode, String dfirstname, 
            String dsurname,  String dbuilding, String dstreet, String dcity,
            String dcounty, String dcountry, String dpostcode, 
            String longitude, String latitude,  String status){

        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_PFIRSTNAME, pfirstname);
        initialValues.put(KEY_PSURNAME, psurname);
        initialValues.put(KEY_PBUILDING, pbuilding);
        initialValues.put(KEY_PSTREET, pstreet);
        initialValues.put(KEY_PCITY, pcity);
        initialValues.put(KEY_PCOUNTY, pcounty);
        initialValues.put(KEY_PCOUNTRY, pcountry);
        initialValues.put(KEY_PPOSTCODE, ppostcode);
        initialValues.put(KEY_DFIRSTNAME, dfirstname);
        initialValues.put(KEY_DSURNAME, dsurname);
        initialValues.put(KEY_DBUILDING, dbuilding);
        initialValues.put(KEY_DSTREET, dstreet);
        initialValues.put(KEY_PCITY, pcity);
        initialValues.put(KEY_DCOUNTY, dcounty);
        initialValues.put(KEY_DCOUNTRY, dcountry);
        initialValues.put(KEY_DPOSTCODE, dpostcode);
        initialValues.put(KEY_LONGITUDE, longitude);
        initialValues.put(KEY_LATITUDE, latitude);
        initialValues.put(KEY_STATUS, status);

        return db.insert(DATABASE_TABLE, null, initialValues); 
    }

    //---deletes a particular title---
    public boolean deletePackage(long rowId) 
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID + 
                "=" + rowId, null) > 0;
    }

    //---retrieves all the titles---
    public Cursor getAllPackages() 
    {
        return db.query(DATABASE_TABLE, new String[] {
                KEY_ROWID,
                KEY_PFIRSTNAME,
                KEY_PSURNAME,
                KEY_PBUILDING,
                KEY_PSTREET,
                KEY_PCITY,
                KEY_PCOUNTY,
                KEY_PCOUNTRY,
                KEY_PPOSTCODE,
                KEY_DFIRSTNAME,
                KEY_DSURNAME,
                KEY_DBUILDING,
                KEY_DSTREET,
                KEY_DCITY,
                KEY_DCOUNTY,
                KEY_DCOUNTRY,
                KEY_DPOSTCODE,
                KEY_LATITUDE,
                KEY_LONGITUDE,
                KEY_STATUS,
                }, 
                null, 
                null, 
                null, 
                null,
                null);

    }

    //---retrieves a particular title---
    public Cursor getPackage(String rowId) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[] {
                        KEY_ROWID,
                        KEY_PFIRSTNAME,
                        KEY_PSURNAME,
                        KEY_PBUILDING,
                        KEY_PSTREET,
                        KEY_PCITY,
                        KEY_PCOUNTY,
                        KEY_PCOUNTRY,
                        KEY_PPOSTCODE,
                        KEY_DFIRSTNAME,
                        KEY_DSURNAME,
                        KEY_DBUILDING,
                        KEY_DSTREET,
                        KEY_DCITY,
                        KEY_DCOUNTY,
                        KEY_DCOUNTRY,
                        KEY_DPOSTCODE,
                        KEY_LATITUDE,
                        KEY_LONGITUDE,
                        KEY_STATUS
                        }, 
                        KEY_ROWID + "=" + rowId, 
                        null,
                        null, 
                        null, 
                        null, 
                        null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //---updates a package location---
    public boolean updateLocation(long rowId, String longitude, String latitude) 
    {
        ContentValues args = new ContentValues();
        args.put(KEY_LONGITUDE, longitude);
        args.put(KEY_LATITUDE, latitude);;
        return db.update(DATABASE_TABLE, args, 
                         KEY_ROWID + "=" + rowId, null) > 0;
    }

    //---updates a package status---
    public boolean updateStatus(long rowId, String status) 
    {
        ContentValues args = new ContentValues();
        args.put(KEY_STATUS, status);
        return db.update(DATABASE_TABLE, args, 
                         KEY_ROWID + "=" + rowId, null) > 0;
    }
}

This is the code that tries to insert some data into my package table:

package com.fyp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ScreenNewPackage2 extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newpackage2);

        DBAdapter db = new DBAdapter(this);

        db.open();        
         long id;
         id = db.insertPackage(
                    "John",
                    "Doe",
                    "1",
                    "Imagination Road",
                    "Super Town",
                    "Berkshire",
                    "UK",
                    "AB12CD",
                    "Mary",
                    "Doe",
                    "24",
                    "Invisible Street",
                    "Large City",
                    "Berkshire",
                    "UK",
                    "AB89CD",
                    "51.43848",
                    "-0.944492",
                    "Picked up");

                Button newPackage2CancelButton = (Button) findViewById(R.id.NewPackage2_Cancel_Button);
                newPackage2CancelButton.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        Intent i = new Intent();
                        i.setClassName("com.fyp", "com.fyp.ScreenNewPackage1");
                        startActivity(i);
                        finish();
            }
        });

        Button newPackage2NextButton = (Button) findViewById(R.id.NewPackage2_Next_Button);
        newPackage2NextButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                Intent i = new Intent();
                i.setClassName("com.fyp", "com.fyp.ScreenMenu");
                startActivity(i);
                finish();
            }
        });
    }
}

and here is the error message:

03-02 16:54:12.139: ERROR/Database(25235): Error inserting dcountry=UK dcounty=Berkshire pfirstname=John pcounty=Berkshire status=Picked up pcountry=UK dbuilding=24 pstreet=Imagination Road psurname=Doe dstreet=Invisible Street pbuilding=1 dpostcode=AB89CD ppostcode=AB12CD longitude=51.43848 latitude=-0.944492 pcity=Super Town dsurname=Doe dfirstname=Mary
03-02 16:54:12.139: ERROR/Database(25235): android.database.sqlite.SQLiteException: table packagetable has no column named psurname: , while compiling: INSERT INTO packagetable(dcountry, dcounty, pfirstname, pcounty, status, pcountry, dbuilding, pstreet, psurname, dstreet, pbuilding, dpostcode, ppostcode, longitude, latitude, pcity, dsurname, dfirstname) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
03-02 16:54:12.139: ERROR/Database(25235):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-02 16:54:12.139: ERROR/Database(25235):     at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
03-02 16:54:12.139: ERROR/Database(25235):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)

Any help would be greatly appreciated, I haven’t tried sqlite3 command run line as I have no idea how to do it etc, even after reading various articles/tutorials.

Update: 3rd March 2011 – New error message.

Thanks to Blumer for identifying one issue, I now receive the following error when running the edited code above:

03-02 17:31:27.849: ERROR/Database(25517): Error inserting dcountry=UK dcounty=Berkshire pfirstname=John pcounty=Berkshire status=Picked up pcountry=UK dbuilding=24 pstreet=Imagination Road psurname=Doe dstreet=Invisible Street pbuilding=1 dpostcode=AB89CD ppostcode=AB12CD longitude=51.43848 latitude=-0.944492 pcity=Super Town dsurname=Doe dfirstname=Mary
03-02 17:31:27.849: ERROR/Database(25517): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed        
03-02 17:31:27.849: ERROR/Database(25517):     at android.database.sqlite.SQLiteStatement.native_execute(Native Method)        
03-02 17:31:27.849: ERROR/Database(25517):     at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55)

Constraint error it seems, any ideas would be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:30:43+00:00Added an answer on May 20, 2026 at 8:30 am

    Your database table does not have a column “psurname”, but you’re trying to insert data into it. You’ll need to look at the structure of your table and either find out what the name that is there is or add the column if it doesn’t already exist.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.