我做了两个表一个为用户和一个为项目,但项目是不是由于某种原因
private static final String TABLE_NAME = "Users";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_USERNAME = "Username";
private static final String COLUMN_PASSWORD = "Password";
// Column and table for new project
private static final String PROJECT_TABLE = "Project";
private static final String COLUMN_id = "id";
private static final String COLUMN_PROJECT_NAME = "projectName";
private static final String COLUMN_DESCRIPTION = "description";
private static final String COLUMN_TEAM = "team";
private static final String COLUMN_PROJECT_MANAGER = "Project_Manager";
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME +
"(" + COLUMN_ID +" INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_USERNAME + " TEXT, " +
COLUMN_PASSWORD + " TEXT);";
String project = "CREATE TABLE " + PROJECT_TABLE +
"(" + COLUMN_id +" INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PROJECT_NAME + " TEXT, " +
COLUMN_DESCRIPTION + " TEXT, " +
COLUMN_TEAM + " TEXT, " +
COLUMN_PROJECT_MANAGER + " TEXT);";
db.execSQL(project);
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + PROJECT_TABLE);
onCreate(db);
}
void addProject(String projectName, String Description, String team, String projectManager){
SQLiteDatabase db = this.getReadableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_PROJECT_NAME, projectName);
cv.put(COLUMN_DESCRIPTION, Description);
cv.put(COLUMN_TEAM, team);
cv.put(COLUMN_PROJECT_MANAGER, projectManager);
long result = db.insert(PROJECT_TABLE, null ,cv);
if(result == -1){
Toast.makeText(context, "Failed to add new project", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "Successfully added new project", Toast.LENGTH_SHORT).show();
}
}
在我的项目java类中,我想可能数据没有正确地插入到数据库中,所以我在db.addProject中编写了代码以获取字符串和文本,但它仍然不起作用
public class Board extends AppCompatActivity {
private EditText projectName, description, team, projectManager;
Button done;
userDB db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_board);
projectName = findViewById(R.id.projectName);
description = findViewById(R.id.description);
team = findViewById(R.id.team);
projectManager = findViewById(R.id.projectManager);
done = findViewById(R.id.button);
done.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String projectname = projectName.getText().toString().trim();
String desc = description.getText().toString().trim();
String Team = team.getText().toString().trim();
String projectmanage = projectManager.getText().toString().trim();
if(projectname.equals("") || desc.equals("") || Team.equals("") ||
projectmanage.equals("")){
Toast.makeText(Board.this, "All Fields are Required", Toast.LENGTH_SHORT).show();
}else{
db = new userDB(Board.this);
db.addProject(projectName.getText().toString().trim(),
description.getText().toString().trim(),
team.getText().toString().trim(),
projectManager.getText().toString().trim());
Intent intent = new Intent(Board.this, MainActivity.class);
startActivity(intent);
finish();
}
}
});
}
}
我试过重新编译一些语法,但结果都是同样的错误。我已经被这个错误卡住了3个小时,我不知道该怎么办了;-;
1条答案
按热度按时间gmxoilav1#
一个错误是,在addProject()方法中,您正在以Read模式(getReadableDatabase())打开数据库。它必须处于写入模式getWritableDatabase()才能将数据插入表中。另外,在插入数据之后,通过调用close()方法来关闭它。