0

大家好,我正在构建一个应用程序,可以在您接到电话时保存位置。不幸的是,我有一个 sql 错误,但我不明白我的错误在哪里。代码是这样的:

文件 storybookdynamicimport.java

 public void CaptureCall() {

    // Declare a telephony manager
    TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    // Declare a phoneStateListener
    PhoneStateListener mPhoneListener;
    // Switching trough various cases
    mPhoneListener = new PhoneStateListener() {
        public void onCallStateChanged(int state, String incomingNumber) {
            String phonenumber=null;
            String currentDate;
            String Loc;
            ContentValues cv = new ContentValues();
            try {
                switch (state) {
                case TelephonyManager.CALL_STATE_RINGING:
                    Log.v(StorybookContentProvider.TAG_LOG,
                            "sei in chiamata");

                    phonenumber= findNameByAddress(StorybookDynamicImport.this, incomingNumber);
                    currentDate = formatDate(System.currentTimeMillis());

                    // Sending a statusbar Notification
                    CallNotification = notificationBuilder("StoryBook",
                            "Ho salvato l'evento chiamata!", null);
                    NotificationManager.notify(1, CallNotification);

                    // Getting the position
                    LocationManager.requestSingleUpdate(criteria,
                            locationListener, looper);
                    Location location = LocationManager
                            .getLastKnownLocation(provider);
                    Loc=GetAddressLocation(location);

                    cv.put(StorybookContentProvider.CONTACT, phonenumber);
                    Log.v(StorybookContentProvider.TAG_LOG, "Salvando" + phonenumber);
                    cv.put(StorybookContentProvider.DATE, currentDate);
                    Log.v(StorybookContentProvider.TAG_LOG, "Data" + currentDate);
                    cv.put(StorybookContentProvider.EVENT_TYPE, "Chiamata");


                    cv.put(StorybookContentProvider.WHERE, "cazzi miei!");
                    Log.v(StorybookContentProvider.TAG_LOG, "DOVE" + Loc);
                    getContentResolver().insert(StorybookContentProvider.CONTENT_URI, cv);



                    break;
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    };

    // Setting listener
    tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}



private String formatDate(long date) {

    Date realdate = new Date(date);
    DateFormat df = new DateFormat();
    String format = (String) df.format("dd/MM/yyyy", realdate);
    //Log.v(StorybookContentProvider.TAG_LOG, "data della chiamata" + format);

    return format;
}

private String findNameByAddress(Context ct, String addr) {
    Uri myPerson = Uri.withAppendedPath(
            ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,
            Uri.encode(addr));
    String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };
    Cursor cursor = ct.getContentResolver().query(myPerson, projection,
            null, null, null);

    if (cursor.moveToFirst()) {
        String name = cursor
                .getString(cursor
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        Log.e("", "Found contact name");
        cursor.close();

        return name;
    }

    cursor.close();
    Log.e("", "Not Found contact name");

    return addr;
}

文件 storybookcontentprovider.java

public class MySQLiteOpenHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "Storybook.db";
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_TABLE_CONTACT = "contact_data";


    public MySQLiteOpenHelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    // SQL statement to create a new database.
    /*
     * private static final String DATABASE_CREATE = "create table " +
     * DATABASE_TABLE + " (" + KEY_ID +
     * " integer primary key autoincrement, " + CONTACT + " text not null, "
     * + CONTACT_ID + "text" + EVENT_TYPE + "text);";
     */

    // Called when no database exists in disk and the helper class needs
    // to create a new one.
    @Override
    public void onCreate(SQLiteDatabase db) {
        StringBuilder createQuery = new StringBuilder();
        createQuery.append("CREATE TABLE \"" + DATABASE_TABLE_CONTACT + "\" (");
        createQuery.append("        \"" + KEY_ID + "\" INTEGER PRIMARY KEY AUTOINCREMENT,");
        createQuery.append("        \"" + CONTACT + "\" TEXT,");
        createQuery.append("        \"" + EVENT_TYPE + "\" TEXT,");
        createQuery.append("        \"" + URI + "\" TEXT,");
        createQuery.append("        \"" + DATE + "\" DATE,");
        createQuery.append("        \"" + WHERE + "\" TEXT");
        createQuery.append(")");

        db.execSQL(createQuery.toString());

        Log.v(TAG_LOG, "database succesfully created");
    }


@Override
public Uri insert(Uri uri, ContentValues values) {
    // Open a read / write database to support the transaction.
    SQLiteDatabase db = myOpenHelper.getWritableDatabase();

    // To add empty rows to your database by passing in an empty Content
    // Values
    // object, you must use the null column hack parameter to specify the
    // name of
    // the column that can be set to null.
    String nullColumnHack = null;

    // Insert the values into the table
    long id = db.insert(MySQLiteOpenHelper.DATABASE_TABLE_CONTACT, nullColumnHack,
            values);

    if (id > -1) {
        // Construct and return the URI of the newly inserted row.
        Uri insertedId = ContentUris.withAppendedId(CONTENT_URI, id);

        // Notify any observers of the change in the data set.
        getContext().getContentResolver().notifyChange(insertedId, null);

        return insertedId;
    } else
        return null;
}

我有这个错误:

10-29 10:51:00.128: I/SqliteDatabaseCpp(896): sqlite returned: error code = 1, msg = near "where": syntax error, db=/data/data/com.paki.storybook/databases/Storybook.db
10-29 10:51:02.658: E/SQLiteDatabase(896): Error inserting where=cazzi miei! date=29/10/2012 contact=Pasquale Lodise event_type=Chiamata
10-29 10:51:02.658: E/SQLiteDatabase(896): android.database.sqlite.SQLiteException: near "where": syntax error: , while compiling: INSERT INTO contact_data(where,date,contact,event_type) VALUES (?,?,?,?)
10-29 10:51:02.658: E/SQLiteDatabase(896):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
4

1 回答 1

4

where是 SQL 保留字。将您的领域称为其他名称。

于 2012-10-29T11:03:01.770 回答