数据库表插入,例外19和物理位置

时间:2011-06-17 01:18:04

标签: android sqlite

我已经创建了一个数据库助手,就像在这个链接上指定的那样: Creating and Using Databases in Android

我已通过USB连接在我的ACER安卓平板电脑上通过程序进行了测试。我打开了我的数据库 - 我想 - 并关闭它。

但是,我无法插入表格:我得到-1的返回值,异常代码为19.

AND:我在平板电脑上找不到数据库。 / data 目录为空。

我已经在本网站和网络上阅读了我在SQL Lite和数据库上可以找到的所有内容。没有任何作用,没有任何解释。

问题1:什么是例外19,我该如何解决? 问题2:为什么我不能在平板电脑上找到我的数据库?

感谢。

根据要求:这是我的dbHelper代码。我不得不削减它,因为原件太大而无法提交:

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 {

    private static final String TAG = "DBAdapter";
    private static final String DATABASE_NAME = "vardata";
    private static final String DATABASE_TABLE_1 = "inspection";
    private static final String DATABASE_TABLE_2 = "fedexvehicles";
    private static final String DATABASE_TABLE_3 = "attachments";
    private static final String DATABASE_TABLE_4 = "nonvehicles";
    private static final String DATABASE_TABLE_5 = "property";
    private static final String DATABASE_TABLE_6 = "people";
    private static final String DATABASE_TABLE_7 = "photos";
    private static final String DATABASE_TABLE_8 = "notes";
    private static final int DATABASE_VERSION = 1;
    public static final String KEY_ROW_ID = "_id";
    public static final String KEY_INSPECTION_ID = "_id";
    public static final String KEY_INSPECTION_ACCDATE = "accdate";
    public static final String KEY_INSPECTION_ACCTIME = "acctime";
    public static final String KEY_INSPECTION_NOTIFIED = "notified";
    public static final String KEY_INSPECTION_ARRIVED = "arrived";
    public static final String KEY_INSPECTION_ACCTYPE = "acctype";
    public static final String KEY_INSPECTION_GATE = "gate";
    public static final String KEY_INSPECTION_ROADSURFACE = "roadsurface";
    public static final String KEY_INSPECTION_ROADTYPE = "roadtype";
    public static final String KEY_INSPECTION_WEATHER = "weather";
    public static final String KEY_INSPECTION_TRAFFIC = "traffic";
    public static final String KEY_INSPECTION_LIGHTING = "lighting";
    public static final String KEY_INSPECTION_HAZMAT = "hazmat";
    public static final String KEY_INSPECTION_MANAGER = "manager";
    public static final String KEY_INSPECTION_SPEEDLIMIT = "speedlimit";
    public static final String KEY_INSPECTION_POLICE = "police";
    public static final String KEY_INSPECTION_POLICEDEPARTMENT = "policedepartment";
    public static final String KEY_INSPECTION_POLICEOFFICER = "policeofficer";
    public static final String KEY_INSPECTION_POLICEBADGE = "policebadge";
    public static final String KEY_INSPECTION_POLICEREPORT = "policereport";
    public static final String KEY_INSPECTION_FEDEXVEHICLES = "fedexvehicles";
    public static final String KEY_INSPECTION_ATTACHED = "attached";
    public static final String KEY_INSPECTION_NONVEHICLES = "nonvehicles";
    public static final String KEY_INSPECTION_PROPERTY = "property";
    public static final String KEY_INSPECTION_INJURED = "injured";
    public static final String KEY_INSPECTION_WITNESSES = "witnesses";
    public static final String KEY_INSPECTION_PHOTOS = "photos";
    public static final String KEY_INSPECTION_COMPLETED = "completed";
    //I created constants for all the table fields, but removed them here due to size

    private static final String DATABASE_CREATE_TABLE_1 =
        "create table inspection (_id integer primary key autoincrement, "
        + "accdate text not null, acctime text not null, " 
        + "notified text not null, arrived text not null, "
        + "acctype text not null, location text not null, "
        + "gate text not null, roadcondition text not null, "
        + "roadsurface text not null, roadtype text not null, "
        + "weather text not null, traffic text not null, "
        + "lighting text not null, hazmat text not null, "
        + "manager text not null, speedlimit text not null, "
        + "police text not null, policedepartment text not null, "
        + "policeofficer text not null, policebadge text not null, "
        + "policereport text not null, fedexvehicles text not null, "
        + "attached text not null, nonvehicles text not null, "
        + "property text not null, injured text not null, "
        + "witnesses text not null, photos text not null, "
    + "completed text not null);";

    private static final String DATABASE_CREATE_TABLE_2 =
        "create table fedexvehicles (_id integer primary key autoincrement, "
        + "investigationkey text not null, vehiclenumber text not null, " 
        + "vehicletype text not null, assetnumber text not null, "
        + "vehicleaction text not null, speed text not null, "
        + "interaction text not null, objectstruck text not null, "
        + "damagetype text not null, damagelocationgeneral text not null, "
        + "damagelocationspecific text not null, guideperson text not null, "
        + "passengersfed text not null, passengersnon text not null, "
        + "wheeledgse text not null, carriedgse text not null, "
        + "damagedattachments text not null, driver text not null, "
        + "driverslicense text not null, driverslicensestate text not null, "
        + "driverslicensedate text not null, seatbelt text not null, "
        + "citation text not null, preventable text not null, "
        + "vio1 text not null, vio2 text not null, "
        + "vio3 text not null, vio4 text not null, "
        + "vio5 text not null, vio6 text not null, "
        + "vio7 text not null, vio8 text not null, "
        + "vio9 text not null, vio10 text not null, "
        + "vio11 text not null, vio12 text not null, "
        + "vio13 text not null, vio14 text not null, "
        + "vio15 text not null, vio16 text not null, "
        + "vio17 text not null, vio18 text not null, "
        + "vio19 text not null, vio20 text not null, "
        + "vio21 text not null, vio22 text not null, "
        + "completed text not null);";

    private static final String DATABASE_CREATE_TABLE_3 =
        "create table attachments (_id integer primary key autoincrement, "
        + "investigationkey text not null, vehiclekey text not null, " 
        + "attachmenttype text not null, assetnumber text not null, "
        + "interaction text not null, objectstruck text not null, "
        + "damagetype text not null, damagelocationgeneral text not null, "
        + "damagelocationspecific text not null, dollyonstring text not null, "
        + "positiononstring text not null, positionondolly text not null, "
        + "attachmentnumber text not null, completed text not null);";

    private static final String DATABASE_CREATE_TABLE_4 =
        "create table nonvehicles (_id integer primary key autoincrement, "
        + "investigationkey text not null, vehiclenumber text not null, " 
        + "vin text not null, text vehmake not null, "
        + "vehmodel text not null, vehyear text not null, "
        + "plate text not null, platestate text not null, "
        + "vehicleaction text not null, speed text not null, "
        + "damagetype text not null, damagelocation text not null, "
        + "damagepart text not null, towed text not null, "
        + "owner text not null, insurance text not null, "
        + "insurancephone text not null, policy text not null, "
        + "policydate text not null, driver text not null, "
        + "driverslicense text not null, driverslicensestate text not null, "
        + "driverslicensedate text not null, citation text not null, "
        + "passengersfed text not null, passengersnon text not null, "
        + "completed text not null);";

    private static final String DATABASE_CREATE_TABLE_5 =
        "create table property (_id integer primary key autoincrement, "
        + "investigationkey text not null, owner text not null, " 
        + "propertytype text not null, propertydamage text not null, "
        + "propertynumber text not null, completed text not null);";

    private static final String DATABASE_CREATE_TABLE_6 =
        "create table people (_id integer primary key autoincrement, "
        + "investigationkey text not null, persontype text not null, " 
        + "employeeid text not null, personname text not null, "
        + "personaddress text not null, personcity text not null, "
        + "personstate text not null, personzip text not null, "
        + "personphone text not null, injured text not null);";

    private static final String DATABASE_CREATE_TABLE_7 =
        "create table photos (_id integer primary key autoincrement, "
        + "investigationkey text not null, photofilename text not null, " 
        + "photolocation text not null, photodirection text not null, "
        + "photocaption text not null);";

    private static final String DATABASE_CREATE_TABLE_8 =
        "create table notes (_id integer primary key autoincrement, "
        + "investigationkey text not null, notetext 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_TABLE_1);
            db.execSQL(DATABASE_CREATE_TABLE_2);
            db.execSQL(DATABASE_CREATE_TABLE_3);
            db.execSQL(DATABASE_CREATE_TABLE_4);
            db.execSQL(DATABASE_CREATE_TABLE_5);
            db.execSQL(DATABASE_CREATE_TABLE_6);
            db.execSQL(DATABASE_CREATE_TABLE_7);
            db.execSQL(DATABASE_CREATE_TABLE_8);
        }

        @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 investigation");
            db.execSQL("DROP TABLE IF EXISTS fedexvehicles");
            db.execSQL("DROP TABLE IF EXISTS attachments");
            db.execSQL("DROP TABLE IF EXISTS nonvehicles");
            db.execSQL("DROP TABLE IF EXISTS property");
            db.execSQL("DROP TABLE IF EXISTS people");
            db.execSQL("DROP TABLE IF EXISTS photos");
            db.execSQL("DROP TABLE IF EXISTS notes");
            onCreate(db);
        }
    }    


//---opens the database---

    public DBAdapter open() throws SQLException 
    {


        db = DBHelper.getWritableDatabase();



        return this;
    }


//---closes the database---    

    public void close() 
    {
        DBHelper.close();
    }


//---insert an inspection into the database---

    public long insertinspection(String acDat, String acNot) 
    {

        ContentValues initialValues = new ContentValues();
        try{
        initialValues.put(KEY_INSPECTION_ACCDATE, acDat);        
        initialValues.put(KEY_INSPECTION_ACCTIME, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_NOTIFIED, "acNot");
        initialValues.put(KEY_INSPECTION_ARRIVED, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_ACCTYPE, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_GATE, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_ROADSURFACE, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_ROADTYPE, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_WEATHER, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_TRAFFIC, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_LIGHTING, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_HAZMAT, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_MANAGER, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_SPEEDLIMIT, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_POLICE, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_POLICEDEPARTMENT, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_POLICEOFFICER, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_POLICEBADGE, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_POLICEREPORT, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_FEDEXVEHICLES, ONE_STRING);
        initialValues.put(KEY_INSPECTION_ATTACHED, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_NONVEHICLES, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_PROPERTY, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_INJURED, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_WITNESSES, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_PHOTOS, EMPTYSTRING);
        initialValues.put(KEY_INSPECTION_COMPLETED, FALSE_STRING);
        } catch (Exception e)       
        {       
            Log.e("ERROR", "DATABASE ERROR: " + e.toString());
            e.printStackTrace();
        }

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

    //---insert a FedEx Vehicle into the database---

    public long insertFedVeh(String inspectionKey, String vehiclenumber) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_FEDVEHICLE_INVESTIGATIONKEY, inspectionKey);
        initialValues.put(KEY_FEDVEHICLE_VEHICLENUMBER, vehiclenumber);
        initialValues.put(KEY_FEDVEHICLE_VEHICLETYPE, "");
        initialValues.put(KEY_FEDVEHICLE_ASSETNUMBER, "");
        initialValues.put(KEY_FEDVEHICLE_VEHICLEACTION, "");
        initialValues.put(KEY_FEDVEHICLE_SPEED, "");
        initialValues.put(KEY_FEDVEHICLE_INTERACTION, "");
        initialValues.put(KEY_FEDVEHICLE_OBJECTSTRUCK, "");
        initialValues.put(KEY_FEDVEHICLE_DAMAGETYPE, "");
        initialValues.put(KEY_FEDVEHICLE_DAMAGELOCATIONGENERAL, "");
        initialValues.put(KEY_FEDVEHICLE_DAMAGELOCATIONSPECIFIC, "");
        initialValues.put(KEY_FEDVEHICLE_GUIDEPERSON, "");
        initialValues.put(KEY_FEDVEHICLE_PASSENGERSFED, "0");
        initialValues.put(KEY_FEDVEHICLE_PASSENGERSNON, "0");
        initialValues.put(KEY_FEDVEHICLE_WHEELEDGSE, "0");
        initialValues.put(KEY_FEDVEHICLE_CARRIEDGSE, "0");
        initialValues.put(KEY_FEDVEHICLE_DAMAGEDATTACHMENTS, "0");
        initialValues.put(KEY_FEDVEHICLE_DRIVER, "");
        initialValues.put(KEY_FEDVEHICLE_DRIVERSLICENSE, "");
        initialValues.put(KEY_FEDVEHICLE_DRIVERSLICENSESTATE, "");
        initialValues.put(KEY_FEDVEHICLE_DRIVERSLICENSEDATE, "");
        initialValues.put(KEY_FEDVEHICLE_SEATBELT, "");
        initialValues.put(KEY_FEDVEHICLE_CITATION, "");
        initialValues.put(KEY_FEDVEHICLE_PREVENTABLE, "");
        initialValues.put(KEY_FEDVEHICLE_VIO1, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO2, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO3, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO4, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO5, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO6, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO7, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO8, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO9, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO10, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO11, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO12, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO13, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO14, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO15, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO16, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO17, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO18, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO19, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO20, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO21, "false");
        initialValues.put(KEY_FEDVEHICLE_VIO22, "false");
        initialValues.put(KEY_FEDVEHICLE_COMPLETED, "false");
        return db.insert(DATABASE_TABLE_2, null, initialValues);
    }

    //---I also created methods to insert each table, but removed them here for size ---

   //---deletes a particular inspection---

    public boolean deleteInspection(long rowId) 
    {
        return db.delete(DATABASE_TABLE_1, KEY_INSPECTION_ID + 
            "=" + rowId, null) > 0;
    }

    //---deletes a particular FedEx Vehicle---

    public boolean deleteFedVehicle(long rowId) 
    {
        return db.delete(DATABASE_TABLE_2, KEY_FEDVEHICLE_ID + 
            "=" + rowId, null) > 0;
    }

    //---deletes a particular Attachment---

    public boolean deleteAttach(long rowId) 
    {
        return db.delete(DATABASE_TABLE_3, KEY_ATTACHMENTS_ID + 
            "=" + rowId, null) > 0;
    }

    //---deletes a particular Non FedEx Vehicle---

    public boolean deleteNonVehicle(long rowId) 
    {
        return db.delete(DATABASE_TABLE_4, KEY_NONVEHICLES_ID + 
            "=" + rowId, null) > 0;
    }

    //---deletes a particular Property---

    public boolean deleteProperty(long rowId) 
    {
        return db.delete(DATABASE_TABLE_5, KEY_PROPERTY_ID + 
            "=" + rowId, null) > 0;
    }

    //---deletes a particular Person---

    public boolean deletePeople(long rowId) 
    {
        return db.delete(DATABASE_TABLE_6, KEY_PEOPLE_ID + 
            "=" + rowId, null) > 0;
    }

    //---deletes a particular Photo---
    public boolean deletePhoto(long rowId) 
    {
        return db.delete(DATABASE_TABLE_7, KEY_PHOTOS_ID + 
            "=" + rowId, null) > 0;
    }

    //---deletes a particular Note---
    public boolean deleteNote(long rowId) 
    {
        return db.delete(DATABASE_TABLE_8, KEY_NOTES_ID + 
            "=" + rowId, null) > 0;
    }

    //---retrieves all the Inspections---

    public Cursor getAllInspections() 
    {
        return db.query(DATABASE_TABLE_1, new String[] {
                KEY_INSPECTION_ID, 
                KEY_INSPECTION_ACCDATE,
                KEY_INSPECTION_NOTIFIED}, 
                null, 
                null, 
                null, 
                null, 
                null);
    }

    //---retrieves all Persons---

    public Cursor getAllPeople(String investigationKey) 
    {
        return db.query(DATABASE_TABLE_6, new String[] {
                KEY_PEOPLE_PERSONNAME, 
                KEY_PEOPLE_EMPLOYEEID}, 
                null, 
                null, 
                null, 
                null, 
                null);
    }

    //---retrieves all Employees---

    public Cursor getAllEmployees(String investigationKey) 
    {
        return db.query(DATABASE_TABLE_6, new String[] {
                KEY_PEOPLE_PERSONNAME, 
                KEY_PEOPLE_EMPLOYEEID}, 
                KEY_PEOPLE_EMPLOYEEID + "<> 0", 
                null, 
                null, 
                null, 
                null);
    }

    //---retrieves all Non Persons---

    public Cursor getAllPersons(String investigationKey) 
    {
        return db.query(DATABASE_TABLE_6, new String[] {
                KEY_PEOPLE_PERSONNAME}, 
                KEY_PEOPLE_EMPLOYEEID + "= 0", 
                null, 
                null, 
                null, 
                null);
    }

    //---retrieves a particular inspection---

    public Cursor getInspection(long investigationKey) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE_1, null, 
                        KEY_INSPECTION_ID + "=" + investigationKey, 
                        null,
                        null, 
                        null, 
                        null, 
                        null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //---retrieves a particular fedexvehicle---
    public Cursor getFedVehicle(String investigationKey, String vehicleNumber) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE_2, null, 
                        KEY_FEDVEHICLE_INVESTIGATIONKEY + "=" + investigationKey+" AND "+KEY_FEDVEHICLE_VEHICLENUMBER+"="+vehicleNumber, 
                        null,
                        null, 
                        null, 
                        null, 
                        null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

  //---retrieves a particular attachment---

    public Cursor getAttachment(String investigationKey, String vehicleNumber, String attachmentNumber) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE_3, null, 
                        KEY_ATTACHMENTS_INVESTIGATIONKEY + "=" + investigationKey+" AND "+KEY_ATTACHMENTS_VEHICLEKEY+"="+vehicleNumber+" AND "+KEY_ATTACHMENTS_ATTACHMENTNUMBER+"="+attachmentNumber, 
                        null,
                        null, 
                        null, 
                        null, 
                        null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //---retrieves a particular NonFedExVehicle---

     public Cursor getNonVehicle(String investigationKey, String vehicleNumber) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE_4, null, 
                        KEY_NONVEHICLES_INVESTIGATIONKEY + "=" + investigationKey+" AND "+KEY_NONVEHICLES_VEHICLENUMBER+"="+vehicleNumber, 
                        null,
                        null, 
                        null, 
                        null, 
                        null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //---retrieves a particular property---

    public Cursor getProperty(String investigationKey, String propertyNumber) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE_5, null, 
                        KEY_PROPERTY_INVESTIGATIONKEY + "=" + investigationKey+" AND "+KEY_PROPERTY_PROPERTYNUMBER+"="+propertyNumber+" AND "+KEY_ATTACHMENTS_ATTACHMENTNUMBER+"="+propertyNumber, 
                        null,
                        null, 
                        null, 
                        null, 
                        null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //---retrieves a particular Person---

    public Cursor getPerson(String investigationKey, String personName, String employeeID) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE_6, null, 
                        KEY_PEOPLE_INVESTIGATIONKEY + "=" + investigationKey + " AND " + KEY_PEOPLE_EMPLOYEEID + " = " + employeeID + " AND " +     KEY_PEOPLE_PERSONNAME + " = " + personName, 
                        null,
                        null, 
                        null, 
                        null, 
                        null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

        //---updates a Field---

    public boolean updateField(long rowId, String tablename, String fieldname, String newdata) 
    {
        ContentValues args = new ContentValues();
        args.put(fieldname, newdata);
        return db.update(tablename, args, 
                         KEY_ROW_ID + "=" + rowId, null) > 0;
    }

}

1 个答案:

答案 0 :(得分:0)

  1. 错误19是您要插入重复的条目,请参阅this answer。但是,如果发布实际导致错误的代码,我们将能够更好地帮助您。
  2. 您无法在无根设备中浏览/ data文件夹。
相关问题