我已经创建了一个SQLite表来保存用户,但在尝试添加用户时出现异常:E/SQLiteLog:(1)没有这样的表:USERS in USERS(LASTNAME,AGE,FIRSTNAME)Values(?,?,?)
数据库处理程序类:
package com.example.as.Data;
导入android.content.Content Values;导入android.content.Context;导入android.analyase.Cursor;导入android.database ase.sqlite.SQLiteDatabase;导入android.database ase.sqlite.SQLiteOpenHelper;导入android.util.Log;
导入com.example.as.Model.User;导入com.example.as.util.Util;
导入java.util.ArrayList;导入java.util.List;
公共类DatabaseHandler扩展了SQLiteOpenHelper{
//the name is the name of the database we will be creating
public DatabaseHandler(Context context) {
super(context, Util.DATABASE_NAME, null, Util.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_USERS_TABLE = "CREATE TABLE " + Util.TABLE_NAME + "("
+ Util.KEY_ID + " INTEGER PRIMARY KEY," + Util.lastName + " TEXT,"
+ Util.lastName + " TEXT," + Util.age + " TEXT" + ")";
db.execSQL(CREATE_USERS_TABLE);
}
//this is called when you upgrade your database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String DROP_TABLE = String.valueOf("DROP TABLE IF EXISTS");
db.execSQL(DROP_TABLE, new String[]{Util.DATABASE_NAME});
onCreate(db);
}
//how to add to database
public void addContact(User user){ //we will use the contact obj to get our shit from content class
SQLiteDatabase db = this.getWritableDatabase();
//structure that allows us to create the items to be added to the database
ContentValues values = new ContentValues();
values.put(Util.firstName, user.getFirstName()); //putting in our values
values.put(Util.lastName, user.getLastName());
values.put(Util.age, user.getAge());
//inserting the ContentValues into the table
db.insert(Util.TABLE_NAME, null, values);
Log.d("DBHandler", "addContact: " + "item added");
db.close();
}
//get a contact by using their id number /* public User getUser(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(Util.TABLE_NAME,new String[]{Util.KEY_ID,
Util.firstName, Util.lastName, Util.age},
Util.KEY_ID + "=?",new String[]{String.valueOf(id)},
null,null,null);
if (cursor!=null)
cursor.moveToFirst(); //start iterating through
//creating and populating the contact obj that we will return
User user = new User();
user.setId(Integer.parseInt(cursor.getString(0)));
user.setFirstName(cursor.getString(1)); //these are row numbers
user.setLastName(cursor.getString(2));
//0 is the id row and 1 is the name row
user.setAge(cursor.getString(3));
return user;
}*/
//lets get all of the contacts
public List<User> getAllContacts(){
List<User> userList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
//SQL string to select all contacts
String selectAll = "SELECT * FROM " + Util.TABLE_NAME;
//rawquery built in method allows you to pass in raw sql code
Cursor cursor = db.rawQuery(selectAll,null);
//iterate through the data
if(cursor.moveToFirst()){
do {
User user = new User();
user.setId(Integer.parseInt(cursor.getString(0)));
user.setFirstName(cursor.getString(1));
user.setLastName(cursor.getString(2));
user.setAge(cursor.getString(3));
//add the contact objects to the list
userList.add(user);
}while (cursor.moveToNext());
}
return userList;
}
}
Util类:
> package com.example.as.util;
public class Util {
//database related items (write psf for the options to show up)
public static final int DATABASE_VERSION = 1; //every database needs to have a version
public static final String DATABASE_NAME = "users_db";
public static final String TABLE_NAME = "users"; //we need a table name
//column names
public static final String KEY_ID = "id";
public static final String firstName = "firstName";
public static final String lastName = "lastName";
public static final String age = "age";
}
主要活动:
package com.example.as;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.example.as.Data.DatabaseHandler;
import com.example.as.Model.User;
import org.w3c.dom.Text;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private Button addUserbtn;
private EditText firstNametxt;
private EditText lastNametxt;
private EditText agetxt;
private TextView userstxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHandler db = new DatabaseHandler(MainActivity.this);
addUserbtn = findViewById(R.id.aduserbtn);
firstNametxt= findViewById(R.id.FirstNametxt);
lastNametxt=findViewById(R.id.lastNametxt);
agetxt = findViewById(R.id.agetxt);
userstxt = findViewById(R.id.userstxt);
addUserbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String firstName = firstNametxt.getText().toString();
String lastName = lastNametxt.getText().toString();
String age = agetxt.getText().toString();
User user = new User();
user.setFirstName(firstName);
user.setLastName(lastName);
user.setAge(age);
db.addContact(user);
// List<User> userList =db.getAllContacts(); //adding all users to list
// for (User us: userList
// ) {
Log.d("MainActivity","onCreate" + user.getFirstName());
//}
}
});
}
//changing to all Users Screen
public void ChangeScenes(View view) {
Intent intent = new Intent(this, allUsers.class);
startActivity(intent); //now we use the intent we made before
}
}
1条答案
按热度按时间6ojccjat1#
我想你在CREATE TABLE QUERY中有一个问题,请检查一下,在“App Inspector”中添加数据之前,你的TABLE是否已经创建?
如果表创建。然后,您需要升级数据库版本。
您的创建表查询
试试这个
您会遇到这个问题,因为您在CREATE TABLE QUERY中写了两次姓氏。