它没有编译,我也不知道为什么... sqlite的新手,试图在栈溢出时遵循某个问题的答案,但无法弄清楚。模态类是MainDataHelper
代码
MainDataHelper myDatabaseHelper = new MainDataHelper(getActivity());
myDatabaseHelper.openDataBase();
String text = myDatabaseHelper.getMostMessagesSent(); //this is the method to query
myDatabaseHelper.close();
mMostMessagesSent.setText(text);
mMostMessagesSent.setTextColor(Color.WHITE);
助手
public class MainDataHelper extends Activity {
private int TotalMessagesSent;
private int TotalMessagesRecieved;
private int TotalMessages;
private String TotalTimeSpent;
private String MostMessagesSent;
private String MostMessagesRecieved;
private String MostTexted;
private String MostTimeSpent;
private int QuizTaken;
private int QuizTakers;
private int Reviewed;
private int Reviews;
public MainDataHelper() {
TotalMessagesSent = 0;
TotalMessagesRecieved = 0;
TotalMessages = 0;
TotalTimeSpent = "";
MostMessagesSent = "";
MostMessagesRecieved = "";
MostTexted = "";
MostTimeSpent = "";
QuizTaken = 0;
QuizTakers = 0;
Reviewed = 0;
Reviews = 0;
}
public MainDataHelper( int TotalMessagesSent, int TotalMessagesRecieved, int TotalMessages, String TotalTimeSpent,String MostMessagesSent, String MostMessagesRecieved, String MostTexted, String MostTimeSpent,int QuizTaken, int QuizTakers, int Reviewed, int Reviews) {
TotalMessagesSent = TotalMessagesSent;
TotalMessagesRecieved = TotalMessagesRecieved;
TotalMessages = TotalMessages;
TotalTimeSpent = TotalTimeSpent;
MostMessagesSent = MostMessagesSent;
MostMessagesRecieved = MostMessagesRecieved;
MostTexted = MostTexted;
MostTimeSpent = MostTimeSpent;
QuizTaken = QuizTaken;
QuizTakers = QuizTakers;
Reviewed = Reviewed;
Reviews = Reviews;
}
public int getTotalMessagesSent() {
return TotalMessagesSent;
}
public int getTotalMessagesRecieved() {
return TotalMessagesRecieved;
}
public int getTotalMessages() {
return TotalMessages;
}
public String getTotalTimeSpent() {
return TotalTimeSpent;
}
public String getMostMessagesSent() {
return MostMessagesSent;
}
public String getMostMessagesRecieved() {
return MostMessagesRecieved;
}
public String getMostTexted() {
return MostTexted;
}
public String getMostTimeSpent() {
return MostTimeSpent;
}
public int getQuizTaken() {
return QuizTaken;
}
public int getQuizTakers() {
return QuizTakers;
}
public int getReviewed() {
return Reviewed;
}
public int getReviews() {
return Reviews;
}
public void setTotalMessagesSent(int TotalMessagesSent) {
TotalMessagesSent = TotalMessagesSent;
}
public void setTotalMessagesRecieved(int TotalMessagesRecieved) {
TotalMessagesRecieved = TotalMessagesRecieved;
}
public void setTotalMessages(int TotalMessages) {
TotalMessages = TotalMessages;
}
public void setTotalTimeSpent(String TotalTimeSpent) { TotalTimeSpent = TotalTimeSpent; }
public void setMostMessagesSent(String MostMessagesSent) {
MostMessagesSent = MostMessagesSent;
}
public void setMostMessagesRecieved(String MostMessagesRecieved) {
MostMessagesRecieved = MostMessagesRecieved;
}
public void setMostTexted(String MostTexted) {
MostTexted = MostTexted;
}
public void setMostTimeSpent(String MostTimeSpent) { MostTimeSpent = MostTimeSpent; }
public void setQuizTaken(int QuizTaken) {
QuizTaken = QuizTaken;
}
public void setQuizTakers(int QuizTakers) {
QuizTakers = QuizTakers;
}
public void setReviewed(int Reviewed) { Reviewed = Reviewed; }
public void setReviews(int Reviews) {
Reviews = Reviews;
}
}
............................................... ................................................... ................................................... ................................................... .................
答案 0 :(得分:1)
由于某些原因,它没有编译。
例如MainDataHelper myDatabaseHelper = new MainDataHelper();
将会编译。
MainDataHelper中没有 openDatabase 方法,您要么必须添加这样的方法,要么取消行myDatabaseHelper.openDataBase();
MainDataHelper中没有 close 方法,您要么必须添加这种方法,要么取消行myDatabaseHelper.close();
考虑到您要使用SQLite数据库,您将使用SQLiteOpenHelper类的子类,该子类将从“活动”或“片段”(甚至很多)中调用。
甚至在考虑编写一行代码之前,您都需要了解您对数据库的要求并进行某种设计(架构)。忽略这一点,并假设(出于演示目的)您想要一个简单的数据库,其中包含一个名为 questions 的表,并且具有一列称为question的列,则以下可能是此类(在这种情况下为 MainDataBaseHelper.java ):-
public class MainDatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASENAME = "question.db"; //<<<<<<<<<< name of the database
public static final int DATABASEVERSION = 1; //<<<<<<<<<< version number of the database
public static final String TABLE_QUESTION = "question"; //<<<<<<<<<< name of the quiz table
public static final String COLUMN_QUESTION_QUESTION = "question";
public MainDatabaseHelper(Context context) {
super(context, DATABASENAME, null, DATABASEVERSION);
}
//<<<<<<<<<< Called ONCE when the database is first created (first time an attempt is made to open if)
@Override
public void onCreate(SQLiteDatabase db) {
String crt_questiontable_sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUESTION + "(" +
COLUMN_QUESTION_QUESTION + " TEXT" +
")";
db.execSQL(crt_questiontable_sql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public long addQuestion(String question) {
ContentValues cv = new ContentValues();
cv.put(COLUMN_QUESTION_QUESTION,question);
return this.getWritableDatabase().insert(TABLE_QUESTION,null,cv);
}
public Cursor getAllQuestions() {
return this.getWritableDatabase().query(TABLE_QUESTION,null,null,null,null,null,null);
}
}
在上面的类存在的情况下,您的代码可以是(作为一个简单示例):-
MainDatabaseHelper myDBHlpr = new MainDatabaseHelper(getActivity()); // Instantiate a MainDatabasehelper object called myDBHlpr
// Add some questions to the questions table
myDBHlpr.addQuestion("This is the first question");
myDBHlpr.addQuestion("This is another question");
myDBHlpr.addQuestion("Yet another question");
// Now get all of the questions
Cursor csr = myDBHlpr.getAllQuestions();
Log.d("DBINFO","There are " + String.valueOf(csr.getCount()) + " questions in the database.");
// Loop through all the questions
while (csr.moveToNext()) {
Log.d("DBINFO",
"Question " +
String.valueOf(csr.getPosition() + 1) +
" is " + csr.getString(csr.getColumnIndex(MainDatabaseHelper.COLUMN_QUESTION_QUESTION))
);
mMostMessagesSent.setText(csr.getString(csr.getColumnIndex(MainDatabaseHelper.COLUMN_QUESTION_QUESTION));
}
csr.close(); //<<<<<<<<<< Should always close Cursor when done with it.
//mMostMessagesSent.setText(text); //<<<<<<<<<< done in the loop through the cursor (for demonstration very likely only the last question will be seen)
mMostMessagesSent.setTextColor(Color.WHITE);
首次运行时,日志将包括:-
11-12 20:17:16.345 1376-1376/? D/DBINFO: There are 3 questions in the database. 11-12 20:17:16.345 1376-1376/? D/DBINFO: Question 1 is This is the first question 11-12 20:17:16.345 1376-1376/? D/DBINFO: Question 2 is This is another question 11-12 20:17:16.345 1376-1376/? D/DBINFO: Question 3 is Yet another question
最后一个问题(可能不是最后一个问题)将显示在TextView中。