Android SQLiteException:没有名为';照片';

Android SQLiteException:没有名为';照片';,android,sqlite,Android,Sqlite,为什么会出现此错误: 12-27 20:37:57.039: E/AndroidRuntime(3798): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=1, data=Intent { (has extras) }} to activity {org.example.dbcontactconsole/org.example.

为什么会出现此错误:

               12-27 20:37:57.039: E/AndroidRuntime(3798): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=1, data=Intent { (has extras) }} to activity {org.example.dbcontactconsole/org.example.dbcontactconsole.DbContactConsole}: android.database.sqlite.SQLiteException: table contactTest1 has no column named photo: , while compiling: INSERT INTO contactTest1(phone, photo, email, name) VALUES(?, ?, ?, ?);
               12-27 20:37:57.039: E/AndroidRuntime(3798):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
                12-27 20:37:57.039: E/AndroidRuntime(3798):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
                12-27 20:37:57.039: E/AndroidRuntime(3798):     at android.app.ActivityThread.access$2000(ActivityThread.java:117) 
                   12-27 20:37:57.039: E/AndroidRuntime(3798):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
                  12-27 20:37:57.039: E/AndroidRuntime(3798):   at android.os.Handler.dispatchMessage(Handler.java:99)
                   12-27 20:37:57.039: E/AndroidRuntime(3798):  at android.os.Looper.loop(Looper.java:123)
                   12-27 20:37:57.039: E/AndroidRuntime(3798):  at android.app.ActivityThread.main(ActivityThread.java:3687)
                   12-27 20:37:57.039: E/AndroidRuntime(3798):  at java.lang.reflect.Method.invokeNative(Native Method)
                   12-27 20:37:57.039: E/AndroidRuntime(3798):  at java.lang.reflect.Method.invoke(Method.java:507)
                   12-27 20:37:57.039: E/AndroidRuntime(3798):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
                   12-27 20:37:57.039: E/AndroidRuntime(3798):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
                   12-27 20:37:57.039: E/AndroidRuntime(3798):  at dalvik.system.NativeStart.main(Native Method)
                   12-27 20:37:57.039: E/AndroidRuntime(3798): Caused by: android.database.sqlite.SQLiteException: table contactTest1 has no column named photo: , while compiling: INSERT INTO contactTest1(phone, photo, email, name) VALUES(?, ?, ?, ?);
                   12-27 20:37:57.039: E/AndroidRuntime(3798):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
                   12-27 20:37:57.039: E/AndroidRuntime(3798):  at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
                     12-27 20:37:57.039: E/AndroidRuntime(3798):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
                      12-27 20:37:57.039: E/AndroidRuntime(3798):   at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
                        12-27 20:37:57.039: E/AndroidRuntime(3798):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
                   12-27 20:37:57.039: E/AndroidRuntime(3798):  at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149)
                    12-27 20:37:57.039: E/AndroidRuntime(3798):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1569)
                   12-27 20:37:57.039: E/AndroidRuntime(3798):  at android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1452)
                    12-27 20:37:57.039: E/AndroidRuntime(3798):     at org.example.dbcontactconsole.DbContactConsole.addContact(DbContactConsole.java:175)
                    12-27 20:37:57.039: E/AndroidRuntime(3798):     at org.example.dbcontactconsole.DbContactConsole.onActivityResult(DbContactConsole.java:136)
                    12-27 20:37:57.039: E/AndroidRuntime(3798):     at android.app.Activity.dispatchActivityResult(Activity.java:3908)
                       12-27 20:37:57.039: E/AndroidRuntime(3798):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
以下是我的db constants.java:

    public static final String NAME = "name";
public static final String PHONE = "phone";
public static final String EMAIL = "email";
public static final String PHOTO="photo";
当我尝试添加联系人时,按下保存按钮时,会出现错误

这是我的.java,我在其中添加了方法:

 @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main); 
      showDatabaseContent();
      lv1 = getListView();

      lv1.setTextFilterEnabled(true);

       lv1.setOnItemClickListener(new OnItemClickListener() {

       public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
           cursor = (Cursor) a.getItemAtPosition(position);
           itemId = cursor.getString(0);
           openOptionsMenu();
           }
       });

       lv1.setOnItemSelectedListener(new OnItemSelectedListener() {

           public void onItemSelected(AdapterView<?> parent, View view, int position, long id){

        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }

       });
   }

   //selected item index from ListView
   public void showDialogItemId(long itemId){
       Toast.makeText(this, "Menu item selected index is" + Long.toString(itemId), Toast.LENGTH_LONG).show();
   }

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

   @Override
   public boolean onOptionsItemSelected(MenuItem item){
       switch (item.getItemId()){
           case R.id.modifyitem:
               if(null != itemId){
                   Bundle contactToModify = new Bundle();
                   contactToModify.putString("cFirstName", cursor.getString(1));
                   contactToModify.putString("cMobilePhone", cursor.getString(2));
                   contactToModify.putString("cEmail", cursor.getString(3));
                   contactToModify.putString("mod_type", "modifyPerson");
                   Intent intent = new Intent(this, ContactDetails.class);
                   intent.setClass(this, ContactDetails.class);
                   intent.putExtras(contactToModify);
                   startActivityForResult(intent, CONTACT_MODIFIED);
               }else{
                   Toast.makeText(this, "Select Contact to modify", Toast.LENGTH_LONG).show();
               }
               break;
           case R.id.additem:

               Intent i = new Intent(this, ContactDetails.class);
               Bundle bun = new Bundle();
               bun.putString("mod_type", "addPerson");
               i.setClass(this, ContactDetails.class);
               i.putExtras(bun);
               startActivityForResult(i, CONTACT_ADDED);
               break;

           case R.id.removeitem:
               if(null != itemId){
                   removeContact(itemId);
                   showDatabaseContent();
               }
               else{
                   Toast.makeText(this, "Select Contact to delete", Toast.LENGTH_LONG).show();
               }
               break;
           case R.id.search:
               Intent j =new Intent(this,Search.class);
               j.setClass(this, Search.class);
               startActivity(j);
               break;
       }
       return true;
   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent intent){
       // See which child activity is calling us back.
       switch (resultCode) {
           case CONTACT_ADDED:
               // This is the standard resultCode that is sent back if the
               // activity crashed or didn't doesn't supply an explicit result.
               if (resultCode == RESULT_FIRST_USER){
                   Bundle bundle = new Bundle();
                   bundle = intent.getBundleExtra("contactData");
                   addContact(bundle);
                   showDatabaseContent();
               } 
               else{
                   Toast.makeText(this, "CANCEL CONTACT BUTTON PRESSED", Toast.LENGTH_LONG).show();
               }
               break;
           case CONTACT_MODIFIED:
               if (resultCode == 2){
                   Bundle bundle = new Bundle();
                   bundle = intent.getBundleExtra("contactData");
                   modifyContact(bundle);
                   showDatabaseContent();
               }
               else{
                   Toast.makeText(this, "MODIFY CONTACT FAILED", Toast.LENGTH_LONG).show();
               }
               break;
           default:
               break;
       }
   }

 //method removes item from database
   private void removeContact(String itemId){
       db = contacts.getWritableDatabase();
       db.delete(DbConstants.TABLE_NAME, "_ID=" + itemId, null);

   }

   private void addContact(Bundle bundle) {
          // Insert a new record into the Events data source.
          // You would do something similar for delete and update.
          db = contacts.getWritableDatabase();
          ContentValues vals = new ContentValues();
          vals.put(DbConstants.NAME, bundle.getString("contactFirstName"));
          vals.put(DbConstants.PHONE, bundle.getString("contactMobilePhone"));
          vals.put(DbConstants.EMAIL, bundle.getString("contactEmail"));
          vals.put(DbConstants.PHOTO,bundle.getString("poza"));
          db.insertOrThrow(DbConstants.TABLE_NAME, null, vals);
       }

 //method should modify existing Contact
   private void modifyContact(Bundle bundle){
       db = contacts.getWritableDatabase();
       ContentValues vals = new ContentValues();
       vals.put(DbConstants.NAME, bundle.getString("contactFirstName"));
       vals.put(DbConstants.PHONE, bundle.getString("contactMobilePhone"));
       vals.put(DbConstants.EMAIL, bundle.getString("contactEmail"));
       db.update(DbConstants.TABLE_NAME, vals, _ID+"="+itemId, null);
   }

   private Cursor getContacts() {
          db = contacts.getReadableDatabase();
          cursor = db.query(DbConstants.TABLE_NAME, FROM, null, null, null,
                null,DbConstants.NAME);
          startManagingCursor(cursor);
          return cursor;
   }

   public void showDatabaseContent(){
       contacts = new DbCreate(this); 
       try {
           cursor = getContacts(); 
           showContacts(cursor); 
       } finally {
           contacts.close(); 
           db.close();
       }
   }

   private void showContacts(Cursor cursor) {
       //set up data binding
       SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, FROM, TO);
       setListAdapter(adapter);
       }
}
我认为错误在这里:
db.insertOrThrow(DbConstants.TABLE_NAME,null,vals);
但不知道为什么

public void onCreate(SQLiteDatabase db) { 
      db.execSQL("CREATE TABLE " + DbConstants.TABLE_NAME + " " +
            "(" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                  DbConstants.NAME + " TEXT NOT NULL," + 
                  DbConstants.PHONE + " TEXT NOT NULL," +
                  DbConstants.PHOTO+
                  DbConstants.EMAIL + ");");
在创建数据库时,不为photo列(以及下面的电子邮件列)指定数据类型。这很可能会导致错误,从而阻止列的生成。在这里指定一个数据类型,您应该很好


在创建数据库时,不为photo列(以及下面的电子邮件列)指定数据类型。这很可能会导致错误,从而阻止列的生成。在这里指定一个数据类型,您应该会很好。

在创建数据库的脚本中没有定义照片和电子邮件的类型。另外,不要忘记使用逗号。

创建数据库的脚本中没有定义照片和电子邮件的类型。另外,请不要忘记使用逗号。

如上所述修复错误。如果它不起作用,请从连接的设备或模拟器卸载你的应用程序,然后运行你的应用程序。

如上所述修复错误。如果它不起作用,请从连接的设备或模拟器卸载你的应用程序,然后运行你的应用程序。

我尝试过,但它不起作用我认为这里的错误是db.insertOrThrow(DbConstants.TABLE_NAME,null,vals);是的,但是我引用的片段是你问题的根源。数据库生成失败;在尝试插入值之前,您不会注意到。然后您会得到这个错误。我更改了表的创建,但是仍然会有错误
public void onCreate(SQLiteDatabase db){db.execSQL(“创建表”+DbConstants.table_NAME++++”(“++_ID+”INTEGER主键自动递增“+DbConstants.NAME+”TEXT NOT NULL“+DbConstants.PHONE+”TEXT NOT NULL“+DbConstants.PHOTO+“TEXT NOT NULL,”+DbConstants.EMAIL+”;”;
您没有正确修复它,您需要在照片列名和“TEXT NOT NULL”之间留一个空格,如下所示:
DbConstants.photo+“TEXT NOT NULL,“
。此外,您的电子邮件列也没有数据类型。这将为不同的列引发相同的错误。我尝试过,但不起作用。我认为错误在这里db.insertOrThrow(DbConstants.TABLE_NAME,null,vals);是的,但我引用的代码段是您的问题的根源。数据库生成会无声地失败;在尝试插入值之前您不会注意到。然后您会收到此错误。我更改了表创建,但仍然存在错误
public void onCreate(SQLiteDatabase db){db.execSQL(“创建表”+DbConstants.table_NAME++”()++u ID+“整型主键自动递增,“+DbConstants.NAME+”TEXT NOT NULL,“+DbConstants.PHONE+”TEXT NOT NULL,“+DbConstants.PHOTO+”TEXT NOT NULL,“+DbConstants.EMAIL+”;”;
您没有正确修复它,您需要在PHOTO列名和“TEXT NOT NULL”之间留一个空格,如下:
DbConstants.PHOTO+“TEXT NOT NULL,”
。此外,您的电子邮件列也没有数据类型。这将为其他列引发相同的错误。
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contactdetails);
     Button sButton = (Button) findViewById(R.id.button1);
     sButton.setOnClickListener(new OnClickListener() {

          public void onClick(View arg0) {
              // in onCreate or any event where your want the user to
              // select a file
              Intent intent = new Intent();
              intent.setType("image/*");
              intent.setAction(Intent.ACTION_GET_CONTENT);
              startActivityForResult(Intent.createChooser(intent,
                      "Select Picture"), SELECT_PICTURE);
          }
      });

    Bundle incomingActionType = getIntent().getExtras();
    operationType = incomingActionType.getString("mod_type");

    if (operationType.equals("addPerson")){

        nameField = (EditText) findViewById(R.id.contactName); 
        mobilePhoneField = (EditText) findViewById(R.id.contactmobile);
        emailField = (EditText) findViewById(R.id.contactemail);

    }

    else if (operationType.equals("modifyPerson")){

        String modifyFirstName = incomingActionType.getString("cFirstName");
        nameField = (EditText) findViewById(R.id.contactName);
        nameField.setText(modifyFirstName);


        String modifyPhoneNumber = incomingActionType.getString("cMobilePhone");
        mobilePhoneField = (EditText) findViewById(R.id.contactmobile);
        mobilePhoneField.setText(modifyPhoneNumber);

        String modifyEmail = incomingActionType.getString("cEmail");
        emailField = (EditText) findViewById(R.id.contactemail);
        emailField.setText(modifyEmail);    
    }


cancelButton = (Button) findViewById(R.id.cancelcontactbutton);
saveContactButton = (Button) findViewById(R.id.savecontactbutton);

saveContactButton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        Intent resultIntent = new Intent();
        Bundle contactData = new Bundle();

            if (nameField.getText().toString().length() == 0){
                new AlertDialog.Builder(ContactDetails.this).setTitle("Error").setMessage("First name field cannot be empty!").setNeutralButton("Close", null).show();
            } else if (mobilePhoneField.getText().toString().length() == 0){
                new AlertDialog.Builder(ContactDetails.this).setTitle("Error").setMessage("Mobile Phone field cannot be empty!").setNeutralButton("Close", null).show();
            } else if (emailField.getText().toString().length() == 0){
                new AlertDialog.Builder(ContactDetails.this).setTitle("Error").setMessage("Email field cannot be empty!").setNeutralButton("Close", null).show();
            } else{
                contactData.putString("contactFirstName", nameField.getText().toString());
                contactData.putString("contactMobilePhone", mobilePhoneField.getText().toString());
                contactData.putString("contactEmail", emailField.getText().toString());
                contactData.putString("poza",poza.getText().toString());
                resultIntent.putExtra("contactData", contactData);

                if (operationType.equals("addPerson")){
                    setResult(RESULT_FIRST_USER, resultIntent);
                }else if (operationType.equals("modifyPerson")){
                    setResult(RESULT_MODIFY_USER, resultIntent);
                }               
                finish();
            }
    }
});

cancelButton.setOnClickListener(new View.OnClickListener(){


    public void onClick(View v) {
        Intent resultIntent = new Intent();
        setResult(RESULT_CANCELED, resultIntent);
        finish();


    }
});

}
public void onActivityResult(int requestCode, int resultCode, Intent data) {  
       if (resultCode == RESULT_OK) {
           if (requestCode == 1) {
               // currImageURI is the global variable I'm using to hold the content:// URI of the image

Uri          currImageURI = data.getData();
               getRealPathFromURI(currImageURI);

           }
         }
       }


    public String getRealPathFromURI(Uri currImageURI) {
       // can post image
       String [] proj={MediaStore.Images.Media.DATA};
       Cursor cursor = managedQuery( currImageURI,
               proj, // Which columns to return
               null,       // WHERE clause; which rows to return (all rows)
               null,       // WHERE clause selection arguments (none)
               null); // Order-by clause (ascending by name)
       int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
       cursor.moveToFirst();


          poza=(EditText)findViewById(R.id.editText1);
           poza.setText(currImageURI.toString());
          return cursor.getString(column_index);

        }
}
public void onCreate(SQLiteDatabase db) { 
      db.execSQL("CREATE TABLE " + DbConstants.TABLE_NAME + " " +
            "(" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                  DbConstants.NAME + " TEXT NOT NULL," + 
                  DbConstants.PHONE + " TEXT NOT NULL," +
                  DbConstants.PHOTO+
                  DbConstants.EMAIL + ");");