java.lang.IllegalArgumentException:列“ voiceID”不存在

时间:2018-10-19 10:16:04

标签: android

这是错误消息:java.lang.IllegalArgumentException:列“ voiceID”不存在

它表示该列不存在,但在create语句中。该活动甚至无法打开。我正在使用Android Studio和Sqlite制作带有数据库的应用程序。我已经上课了。这是数据库助手。

public class ContactDBHelper extends SQLiteOpenHelper {

public static final String TAG = ContactDBHelper.class.getSimpleName();

private static final String DATABASE_NAME = "cally.db";
private static final int DATABASE_VERSION = 1;

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

@Override
public void onCreate(SQLiteDatabase db) {

    String SQL_CREATE_STATEMENT = "CREATE TABLE " + ContactEntry.TABLE_NAME + " ("
            + ContactEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + ContactEntry.COLUMN_VOICE_ID + " TEXT NOT NULL, "
            + ContactEntry.COLUMN_VIDEO_ID + " TEXT NOT NULL, "
            + ContactEntry.COLUMN_CONTACT_NAME + " TEXT NOT NULL, "
            + ContactEntry.COLUMN_CONTACT_NUMBER + " TEXT NOT NULL, "
            + ContactEntry.COLUMN_CONTACT_IMAGE + " TEXT NOT NULL);";

    db.execSQL(SQL_CREATE_STATEMENT);
    Log.v(TAG,"Table Created");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}

这是“合同”类别:

public final class Contract {

private Contract(){}

public static final String CONTENT_AUTHORITY = "com.example.android.cally";
public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
public static final String PATH_CONTACTS = "Contacts";

public static final class ContactEntry implements BaseColumns{

    public static final String CONTENT_LIST_TYPE =
            ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_CONTACTS;


    public static final String CONTENT_ITEM_TYPE =
            ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_CONTACTS;


    public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_CONTACTS);

    public final static String TABLE_NAME = "Contacts";
    public final static String _ID = BaseColumns._ID;
    public final static String COLUMN_VOICE_ID = "voiceID";
    public final static String COLUMN_VIDEO_ID = "videoID";
    public final static String COLUMN_CONTACT_NAME = "name";
    public final static String COLUMN_CONTACT_NUMBER = "number";
    public final static String COLUMN_CONTACT_IMAGE = "imagePath";


}


}

这是Main Activity,插入数据是要插入虚拟数据。

public class MainActivity extends AppCompatActivity {

    FloatingActionButton FAB;
    private String TAG = "MainActivity";
    private ContactDBHelper mDBHelper;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FAB = (FloatingActionButton)findViewById(R.id.floatingActionButton);
        FAB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,NewContact.class);
                startActivity(i);
            }
        });

        mDBHelper = new ContactDBHelper(this);

    }

    private void insertContact() {
        // Gets the database in write mode
        SQLiteDatabase db = mDBHelper.getWritableDatabase();



        Random rand = new Random();
        int randomNum = rand.nextInt((999 - 1) + 1) + 1;

        ContentValues values = new ContentValues();
        values.put(ContactEntry._ID,randomNum);
        values.put(ContactEntry.COLUMN_CONTACT_NAME, "Bob");
        values.put(ContactEntry.COLUMN_CONTACT_NUMBER, "1234567");
        values.put(ContactEntry.COLUMN_CONTACT_IMAGE,"bob.jpg");
        values.put(ContactEntry.COLUMN_VOICE_ID, "4472");
        values.put(ContactEntry.COLUMN_VIDEO_ID, "4471");

//        long newRowId = db.insert(ContactEntry.TABLE_NAME, null, values);


        Uri newUri = getContentResolver().insert(ContactEntry.CONTENT_URI, values);
        displayInfo();

    }


    @Override
    protected void onStart() {
        super.onStart();
        displayInfo();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // User clicked on a menu option in the app bar overflow menu
        switch (item.getItemId()) {
            case R.id.action_insert_dummy_data:
                insertContact();

                displayInfo();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void displayInfo() {
        // Define a projection that specifies which columns from the database
        // you will actually use after this query.
        String[] projection = {
                ContactEntry._ID,
                ContactEntry.COLUMN_CONTACT_NAME,
                ContactEntry.COLUMN_CONTACT_NUMBER,
                ContactEntry.COLUMN_CONTACT_IMAGE};

        // Perform a query on the provider using the ContentResolver.
        Cursor cursor = getContentResolver().query(
                ContactEntry.CONTENT_URI,   // The content URI of the words table
                projection,             // The columns to return for each row
                null,                   // Selection criteria
                null,                   // Selection criteria
                null);                  // The sort order for the returned rows

        TextView displayView = (TextView) findViewById(R.id.tableCount);

        try {
            // Create a header in the Text View that looks like this:
            //
            //
            // In the while loop below, iterate through the rows of the cursor and display
            // the information from each column in this order.
            displayView.setText("The contacts table contains " + cursor.getCount() + " contacts.\n\n");

            displayView.append(ContactEntry._ID + " - " +
                    ContactEntry.COLUMN_CONTACT_NAME + " - " +
                    ContactEntry.COLUMN_CONTACT_NUMBER + " - " +
                    ContactEntry.COLUMN_CONTACT_IMAGE + "\n" +
                    ContactEntry.COLUMN_VOICE_ID + " - " +
                    ContactEntry.COLUMN_VIDEO_ID + "\n");

            // Figure out the index of each column
            int idColumnIndex = cursor.getColumnIndex(ContactEntry._ID);
            int nameColumnIndex = cursor.getColumnIndex(ContactEntry.COLUMN_CONTACT_NAME);
            int numberColumnIndex = cursor.getColumnIndex(ContactEntry.COLUMN_CONTACT_NUMBER);
            int imageColumnIndex = cursor.getColumnIndex(ContactEntry.COLUMN_CONTACT_IMAGE);
            int voiceIDColumnIndex = cursor.getColumnIndexOrThrow(ContactEntry.COLUMN_VOICE_ID);
            int videoIDColumnIndex = cursor.getColumnIndex(ContactEntry.COLUMN_VIDEO_ID);

            // Iterate through all the returned rows in the cursor
            while (cursor.moveToNext()) {
                // Use that index to extract the String or Int value of the word
                // at the current row the cursor is on.
                int currentID = cursor.getInt(idColumnIndex);
                String currentName = cursor.getString(nameColumnIndex);
                String currentNumber = cursor.getString(numberColumnIndex);
                String currentImage = cursor.getString(imageColumnIndex);
                String currentVoiceID = cursor.getString(voiceIDColumnIndex);
                String currentVideoID = cursor.getString(videoIDColumnIndex);
                // Display the values from each column of the current row in the cursor in the TextView
                displayView.append(("\n" + currentID + " - " +
                        currentName + " - " +
                        currentNumber + " - " +
                        currentImage + " - " +
                        currentVoiceID + " - " +
                        currentVideoID));
            }
        } finally {
            // Always close the cursor when you're done reading from it. This releases all its
            // resources and makes it invalid.
            cursor.close();
        }
    }



}

1 个答案:

答案 0 :(得分:1)

使用此

String[] projection = {
            ContactEntry._ID,
            ContactEntry.COLUMN_VOICE_ID,
            ContactEntry.COLUMN_VIDEO_ID,
            ContactEntry.COLUMN_CONTACT_NAME,
            ContactEntry.COLUMN_CONTACT_NUMBER,
            ContactEntry.COLUMN_CONTACT_IMAGE};