Android 在将数据插入mysqlite数据库时遇到问题
嗨,我是android开发新手,需要帮助将我的数据插入MYSQLite数据库。有人能帮我理解这个问题吗?谢谢 使用log cat,这就是它给我的错误 05-02 18:53:05.087:I/数据库(331):sqlite返回:错误代码=1,msg=表疑点没有名为other的列 05-02 18:53:05.097:E/数据库(331):插入其他=8–14国籍=身高=8–14年龄=姓名=嫌疑人=性别=男性头发时出错=V@NAndroid 在将数据插入mysqlite数据库时遇到问题,android,mysql,database,eclipse,Android,Mysql,Database,Eclipse,嗨,我是android开发新手,需要帮助将我的数据插入MYSQLite数据库。有人能帮我理解这个问题吗?谢谢 使用log cat,这就是它给我的错误 05-02 18:53:05.087:I/数据库(331):sqlite返回:错误代码=1,msg=表疑点没有名为other的列 05-02 18:53:05.097:E/数据库(331):插入其他=8–14国籍=身高=8–14年龄=姓名=嫌疑人=性别=男性头发时出错=V@N package com.example.sherlock; impor
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;
导入java.util.List;
导入android.app.Activity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Spinner;
公共类数据库扩展活动实现OnClickListener{
私有编辑文本名称TXT;
私人纺纱工性别;
私人纺纱机时代;
私有编辑文本;
私有编辑文本高度TXT;
私有EditText nationalitytxt;
私有编辑文本;
私有微调器怀疑类型;
私人按钮提交;
专用按钮clearbtn;
//创建活动时调用
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_数据库);
nametxt=(EditText)findViewById(R.id.nametxt);
性别=(微调器)findViewById(R.id.gender);
年龄=(微调器)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=(微调器)findViewById(R.id.age);
submitbtn=(按钮)findViewById(R.id.submitbtn);
submitbtn.setOnClickListener(此);
clearbtn=(按钮)findviewbyd(R.id.clearbtn);
clearbtn.setOnClickListener(此);
}
公共void onClick(视图v){
if(v.getId()==R.id.submitbtn){
字符串providenametxt=nametxt.getText().toString();
String providegender=gender.getSelectedItem().toString();
String provideage=age.getSelectedItem().toString();
字符串providehairtxt=hairtxt.getText().toString();
字符串provideHeightText=heighttxt.getText().toString();
字符串providenationalitytext=nationalitytxt.getText().toString();
字符串provideothertxt=othertxt.getText().toString();
String providesuspecttype=suspecttype.getSelectedItem().toString();
DBAdapter db=新的DBAdapter(此);
db.添加嫌疑犯(新嫌疑犯(providenametxt、providegender、,
provideage,provideairtxt,
ProvideHaightText、ProvidenationAlityText、,
provideothertxt,provideosuspecttype));
//读取所有联系人
日志d(“读取:”,“读取所有联系人…”);
List suspects=db.getAllSuspects();
对于(嫌疑犯cn:嫌疑犯){
字符串log=“Id:”+cn.getID()+”,名称:“+cn.getName()
+,性别:“+cn.getGender()+”,年龄:“+cn.getAge()
+,头发:“+cn.getHair()+”,高度:“+cn.getHeight()
+,国籍:“+cn.GetNational()+”,其他:“+cn.getOther()
+,Suspecttype:“+cn.getSuspect();
//将联系人写入日志
Log.d(“名称:”,Log);
}
//从现有UI中释放
db.close();
完成();
}
}
}
DBAdapter
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();
}
}
package com.example.sherlock;
导入android.content.ContentValues;
导入android.content.Context;
导入android.database.Cursor;
导入android.database.sqlite.SQLiteDatabase;
导入android.database.sqlite.SQLiteOpenHelper;
导入java.util.ArrayList;
导入java.util.List;
公共类DBAdapter扩展了SQLiteOpenHelper{
公共静态最终字符串键\u ROWID=“id”;
公共静态最终字符串键\u NAME=“NAME”;
公共静态最终字符串键\u GENDER=“GENDER”;
公共静态最终字符串键\u HEIGHT=“HEIGHT”;
公共静态最终字符串键\u AGE=“AGE”;
公共静态最终字符串键\u HAIR=“HAIR”;
公共静态最终字符串键\u national=“national”;
公共静态最终字符串键\u SUSPECT=“SUSPECT”;
公共静态最终字符串键\u OTHER=“OTHER”;
私有静态最终字符串数据库\u NAME=“SuspectDB”;
私有静态最终字符串表\u SUSPECTS=“suspect”;
私有静态最终int数据库_VERSION=1;
公共DBAdapter(上下文){
super(上下文、数据库名称、null、数据库版本);
}
//创建表
@凌驾
public void onCreate(SQLiteDatabase db){
字符串CREATE_Suspects_TABLE=“CREATE TABLE”+TABLE_Suspects+”(“
+KEY_ROWID+“整数主键,”
+按键名称+“文本,”
+键_性别+“文本,”
+按键高度+文本
+键+文本
+键_HAIR+“文本,”
+按键+文本
+按键+可疑文本,“
+键_其他+“文本”+”;
execSQL(创建表);
}
//升级数据库
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
//删除旧表(如果存在)
db.execSQL(“如果存在删除表”+表_);
//再次创建表
onCreate(db);
}
/**
*所有CRUD(创建、读取、更新、删除)操作
*/
//增加新嫌疑人
无效添加嫌疑犯(嫌疑犯){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues=新的ContentValues();
value.put(KEY_NAME,suspects.getName());//联系人姓名
value.put(KEY_-GENDER,suspects.getGender());
value.put(KEY_HEIGHT,suspects.getHeight());
value.put(KEY_AGE,suspects.getAge());
value.put(KEY_HAIR,suspects.getHair());
values.put(KEY_national,suspects.getnational());
value.put(KEY_SUSPECT,suspects.getSuspect());
value.put(KEY_OTHER,suspects.getOther());
//插入行
db.insert(表1,空,值);
db.close();//关闭数据库连接
}