0

嗨,我是 android 开发新手,需要帮助将我的数据插入 MYSQLite 数据库,有人可以帮助我理解问题,感谢使用 log cat,这是它给我的错误

05-02 18:53:05.087:I/Database(331):sqlite 返回:错误代码 = 1,msg = 表怀疑没有名为 other 的列 05-02 18:53:05.097:E/Database(331):错误插入其他=8-14国籍=身高=8-14年龄=姓名=嫌疑人=性别=男发=V@N

package com.example.sherlock;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class Database extends Activity implements OnClickListener {

private EditText nametxt;
private Spinner gender;
private Spinner age;
private EditText hairtxt;
private EditText heighttxt;
private EditText nationalitytxt;
private EditText othertxt;
private Spinner suspecttype;
private Button submitbtn;
private Button clearbtn;


//called when activity is created
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_database);

nametxt =(EditText) findViewById(R.id.nametxt);
gender =(Spinner) findViewById(R.id.gender);
age =(Spinner) findViewById(R.id.age);
hairtxt =(EditText) findViewById(R.id.hairtxt);
heighttxt =(EditText) findViewById(R.id.heighttxt);
nationalitytxt =(EditText) findViewById(R.id.nationalitytxt);
othertxt =(EditText) findViewById(R.id.othertxt);
suspecttype =(Spinner) findViewById(R.id.age);

submitbtn =(Button)findViewById(R.id.submitbtn);
submitbtn.setOnClickListener(this);
clearbtn =(Button)findViewById(R.id.clearbtn);
clearbtn.setOnClickListener(this);
}

public void onClick(View v){

   if(v.getId ()==R.id.submitbtn){
  String providenametxt = nametxt.getText().toString();
  String providegender = gender.getSelectedItem().toString();
  String provideage = age.getSelectedItem().toString();
  String providehairtxt = hairtxt.getText().toString();
  String provideheighttxt = heighttxt.getText().toString();
  String providenationalitytxt = nationalitytxt.getText().toString();
  String provideothertxt = othertxt.getText().toString();
  String providesuspecttype = suspecttype.getSelectedItem().toString();


  DBAdapter db = new DBAdapter(this);
  db.addSuspect(new Suspects( providenametxt, providegender, 
  provideage, providehairtxt,                   
  provideheighttxt, providenationalitytxt, 
  provideothertxt, providesuspecttype));
  //reading all contacts
  Log.d("Reading: ", "Reading all Contacts..");
  List<Suspects> suspects = db.getAllSuspects();

  for (Suspects cn : suspects) {
   String log = "Id: "+ cn.getID() +" ,Name: " + cn.getName() 
   +" ,Gender: " +    cn.getGender()  +" ,Age: " + cn.getAge()
   +" ,Hair: " + cn.getHair() +" ,Height: " + cn.getHeight()
   +" ,Nationality: " + cn.getNationality() +" ,Other: " + cn.getOther() 
   +" ,Suspecttype: " + cn.getSuspect();
  //Writing contacts to log
  Log.d("Name: ", log);
   }
   //release from existing UI
   db.close();
  finish();
  }
  }
  }

数据库适配器

package com.example.sherlock;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;

public class DBAdapter extends SQLiteOpenHelper {
public static final String KEY_ROWID = "id";
public static final String KEY_NAME = "name";
public static final String KEY_GENDER = "gender";
public static final String KEY_HEIGHT = "height";
public static final String KEY_AGE = "age";
public static final String KEY_HAIR = "hair";
public static final String KEY_NATIONALITY = "nationality";
public static final String KEY_SUSPECT = "suspect";
public static final String KEY_OTHER = "other";

private static final String DATABASE_NAME = "SuspectDB";
private static final String TABLE_SUSPECTS = "suspect";
private static final int DATABASE_VERSION = 1;


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

//create tables
 @Override
 public void onCreate(SQLiteDatabase db) {
        String CREATE_Suspects_TABLE = "CREATE TABLE " + TABLE_SUSPECTS + "("
                + KEY_ROWID + " INTEGER PRIMARY KEY," 
                + KEY_NAME + " TEXT,"
                + KEY_GENDER + " TEXT," 
                + KEY_HEIGHT + "TEXT,"  
                + KEY_AGE + "TEXT," 
                + KEY_HAIR + "TEXT," 
                + KEY_NATIONALITY + "TEXT,"
                + KEY_SUSPECT + "TEXT," 
                + KEY_OTHER + "TEXT" +")";
        db.execSQL(CREATE_Suspects_TABLE);
    }
// Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_SUSPECTS);

        // Create tables again
        onCreate(db);
    }
    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */

    // Adding new suspect
    void addSuspect(Suspects suspects) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, suspects.getName()); // Contact Name
        values.put(KEY_GENDER, suspects.getGender());
        values.put(KEY_HEIGHT, suspects.getHeight());
        values.put(KEY_AGE, suspects.getAge());
        values.put(KEY_HAIR, suspects.getHair());
        values.put(KEY_NATIONALITY, suspects.getNationality());
        values.put(KEY_SUSPECT, suspects.getSuspect());
        values.put(KEY_OTHER, suspects.getOther());

        // Inserting Row
        db.insert(TABLE_SUSPECTS, null, values);
        db.close(); // Closing database connection
    }

    // Getting single contact
    Suspects getSuspect(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_SUSPECTS, new String[] { KEY_ROWID,
                KEY_NAME, KEY_GENDER, KEY_HEIGHT,
   KEY_AGE, KEY_HAIR,KEY_NATIONALITY,KEY_SUSPECT,
  KEY_OTHER }, KEY_ROWID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        Suspects suspect = new Suspects(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1),
                cursor.getString(2),
                cursor.getString(3),
                cursor.getString(4),
                cursor.getString(5),
                cursor.getString(6),
                cursor.getString(7),
                cursor.getString(8));
        // return suspect
        return suspect;
    }

    // Getting All suspects
    public List<Suspects> getAllSuspects() {
        List<Suspects> SuspectsList = new ArrayList<Suspects>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_SUSPECTS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Suspects suspect = new Suspects();
                suspect.setID(Integer.parseInt(cursor.getString(0)));
                suspect.setName(cursor.getString(1));
                suspect.setGender(cursor.getString(2));
                suspect.setHeight(cursor.getString(3));
                suspect.setAge(cursor.getString(4));
                suspect.setHair(cursor.getString(5));
                suspect.setNationality(cursor.getString(6));
                suspect.setSuspect(cursor.getString(7));
                suspect.setOther(cursor.getString(8));

       // Adding contact to list
                SuspectsList.add(suspect);
            } while (cursor.moveToNext());
        }

        // return Suspect list
        return SuspectsList;
    }

    // Updating single Suspect
    public int updateSuspects(Suspects suspect) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, suspect.getName());
        values.put(KEY_GENDER, suspect.getGender());
        values.put(KEY_HEIGHT, suspect.getHeight());
        values.put(KEY_AGE, suspect.getAge());
        values.put(KEY_HAIR, suspect.getHair());
        values.put(KEY_NATIONALITY, suspect.getNationality());
        values.put(KEY_SUSPECT, suspect.getSuspect());
        values.put(KEY_OTHER, suspect.getOther());

        // updating row
        return db.update(TABLE_SUSPECTS, values, KEY_ROWID + " = ?",
                new String[] { String.valueOf(suspect.getID()) });
    }

    // Deleting single suspect
    public void deleteSuspect(Suspects suspect) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_SUSPECTS, KEY_ROWID + " = ?",
                new String[] { String.valueOf(suspect.getID()) });
        db.close();
    }


    // Getting suspect Count
    public int getSuspectsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_SUSPECTS;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }    
}
4

2 回答 2

0

在表中,嫌疑人没有名为 other 的列。我认为您应该删除数据库文件并再次运行您的应用程序或更新数据库。

于 2013-05-03T09:37:08.130 回答
0

修复了错误必须将数据库重命名为susceptedDB

于 2013-05-04T11:17:49.643 回答