我在将数据插入我的sqlite表时遇到问题,我想添加,更新和删除用户。添加用户时出现以下错误:
E/SQLiteDatabase: Error inserting name=new address=9849 age=text phone_number=123
android.database.sqlite.SQLiteException: table Customer_Info has no column named name (code 1): , while compiling: INSERT INTO Customer_Info(name,address,age,phone_number) VALUES (?,?,?,?)
我的MainActivity:
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private DatabaseHelper dbHelper;
private PersonAdapter adapter;
private String filter = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
populaterecyclerView(filter);
}
private void populaterecyclerView(String filter){
dbHelper = new DatabaseHelper(this);
adapter = new PersonAdapter(dbHelper.personList(filter), this, mRecyclerView);
mRecyclerView.setAdapter(adapter);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.home_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.addMenu:
Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show();
goToAddUserActivity();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void goToAddUserActivity(){
Intent intent = new Intent(MainActivity.this, AddRecordActivity.class);
startActivity(intent);
}
@Override
protected void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
}
} 我的DatabaseHelper.class
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Customers";
private static final int DATABASE_VERSION = 3;
public static final String TABLE_NAME = "Customer_Info";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_PERSON_NAME = "name";
public static final String COLUMN_PERSON_AGE = "age";
public static final String COLUMN_PERSON_PH = "phone_number";
public static final String COLUMN_PERSON_Address = "address";
public DatabaseHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(" CREATE TABLE " + TABLE_NAME + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PERSON_Address + " TEXT NOT NULL, " +
COLUMN_PERSON_NAME + " TEXT NOT NULL, " +
COLUMN_PERSON_AGE + " NUMBER NOT NULL, " +
COLUMN_PERSON_PH + " NUMBER NOT NULL );"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// you can implement here migration process
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
this.onCreate(db);
}
public void saveNewPerson(Person person) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_PERSON_Address, person.getAddress());
values.put(COLUMN_PERSON_NAME, person.getName());
values.put(COLUMN_PERSON_AGE, person.getAge());
values.put(COLUMN_PERSON_PH, person.getPhone_number());
sqLiteDatabase.insert(TABLE_NAME, null, values);
sqLiteDatabase.close();
}
public List<Person> personList(String list) {
String query;
if (list.equals("")) {
//regular query
query = "SELECT * FROM " + TABLE_NAME;
} else {
//filter results by filter option provided
query = "SELECT * FROM " + TABLE_NAME + " ORDER BY " + list;
}
List<Person> peopleLinkedList = new LinkedList<>();
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(query, null);
Person person;
if (cursor.moveToFirst()) {
do {
person = new Person();
person.setId(cursor.getLong(cursor.getColumnIndex(COLUMN_ID)));
person.setName(cursor.getString(cursor.getColumnIndex(COLUMN_PERSON_NAME)));
person.setAge(cursor.getString(cursor.getColumnIndex(COLUMN_PERSON_AGE)));
person.setAddress(cursor.getString(cursor.getColumnIndex(COLUMN_PERSON_Address)));
person.setPhone_number(cursor.getString(cursor.getColumnIndex(COLUMN_PERSON_PH)));
peopleLinkedList.add(person);
} while (cursor.moveToNext());
}
return peopleLinkedList;
}
/**
* Query only 1 record
**/
public Person getPerson(long id) {
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME + " WHERE _id=" + id;
Cursor cursor = db.rawQuery(query, null);
Person receivedPerson = new Person();
if (cursor.getCount() > 0) {
cursor.moveToFirst();
receivedPerson.setAddress(cursor.getString(cursor.getColumnIndex(COLUMN_PERSON_Address)));
receivedPerson.setName(cursor.getString(cursor.getColumnIndex(COLUMN_PERSON_NAME)));
receivedPerson.setAge(cursor.getString(cursor.getColumnIndex(COLUMN_PERSON_AGE)));
receivedPerson.setPhone_number(cursor.getString(cursor.getColumnIndex(COLUMN_PERSON_PH)));
}
return receivedPerson;
}
}
}
添加用户活动:
public class AddRecordActivity extends AppCompatActivity {
private EditText mNameEditText;
private EditText mAgeEditText;
private EditText mAddressEt;
private EditText mPh;
private Button mAddBtn;
private DatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_record);
//init
mNameEditText = (EditText)findViewById(R.id.userName);
mAgeEditText = (EditText)findViewById(R.id.userAge);
mAddressEt = (EditText)findViewById(R.id.addressAdd);
mPh = (EditText)findViewById(R.id.ph_no);
mAddBtn = (Button)findViewById(R.id.addNewUserButton);
//listen to add button click
mAddBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//call the save person method
savePerson();
}
});
}
private void savePerson(){
String address = mAddressEt.getText().toString().trim();
String name = mNameEditText.getText().toString().trim();
String age = mAgeEditText.getText().toString().trim();
String ph = mPh.getText().toString().trim();
dbHelper = new DatabaseHelper(this);
if(name.isEmpty()){
//error name is empty
Toast.makeText(this, "You must enter a name", Toast.LENGTH_SHORT).show();
}
if(age.isEmpty()){
//error name is empty
Toast.makeText(this, "You must enter an age", Toast.LENGTH_SHORT).show();
}
if(address.isEmpty()){
//error name is empty
Toast.makeText(this, "You must enter an occupation", Toast.LENGTH_SHORT).show();
}
if(ph.isEmpty()){
//error name is empty
Toast.makeText(this, "You must enter an image link", Toast.LENGTH_SHORT).show();
}
//create new person
Person person = new Person(address, name, age,ph);
dbHelper.saveNewPerson(person);
//finally redirect back home
// NOTE you can implement an sqlite callback then redirect on success delete
goBackHome();
}
private void goBackHome(){
startActivity(new Intent(AddRecordActivity.this, MainActivity.class));
}
} 我正在制作一个可以在我的应用程序中执行CRUD操作的普通表单,通过在提交表单以添加用户时出现上述错误,有人可以提示我我在做什么错,为什么没有添加用户
答案 0 :(得分:1)
您输入了错误的年龄
Error inserting name=new address=9849 age=text phone_number=123
年龄是数字,您要插入“文本”
您可以通过强制用户输入文本或数字来在XML中进行更改
供您使用的名称和地址edittext
android:inputType="text"
您的年龄和手机使用情况
android:inputType="number"
答案 1 :(得分:-1)
我不知道整体情况,但是在已经创建表之后在表中添加新行时遇到了类似的问题。如果要在创建表后向表中添加新行,请尝试删除该表并重新创建。
删除
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);