这是一个测验应用程序得分将添加到Rank表中,还有一个按钮,通过点击它将表中的分数将被删除..但我工作正常前两次现在它继续崩溃。没有我之前第一次遇到这个错误因为我从begining开始编码。错误在
之下致命的例外:主要 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.kdeepti.navyquiz / com.example.kdeepti.navyquiz.Score}:android.database.sqlite.SQLiteException:no such table:Rank(code 1):,编译时:SELECT * FROM Rank按分数排序DESC LIMIT 10;
表存在于数据库中,我也尝试过SQLiteAssestHelper 我的活动
公共类MainActivity扩展了AppCompatActivity {
Button btnPlay,btnScore, btnExit;
TextView textView;
ImageButton btnShare,info,clear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
info =(ImageButton) findViewById(R.id.info);
btnPlay = (Button) findViewById(R.id.btnPlay);
btnScore = (Button) findViewById(R.id.btnScore);
btnExit = (Button) findViewById(R.id.btnExit);
textView =(TextView) findViewById(R.id.textView);
textView.setText("Main");
clear =(ImageButton) findViewById(R.id.clear);
btnShare=(ImageButton) findViewById(R.id.btnShare);
btnPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), CategorySecond.class);
startActivity(intent);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
finish();
}
});
btnScore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Score.class);
startActivity(intent);
overridePendingTransition(R.anim.push_in, R.anim.push_out);
finish();
}
});
btnExit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Instruction.class);
startActivity(intent);
overridePendingTransition(R.anim.zoom_in, R.anim.zoom_out);
finish();
}
});
info.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), About.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_up, R.anim.slide_down);
finish();
}
});
btnShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT,"My New App");
String applink="Hello,Welcome to NavyQuiz";
intent.putExtra(Intent.EXTRA_TEXT,"Try My new App :"+applink);
startActivity(Intent.createChooser(intent,"Share Via"));
}
});
final DbHelper db=new DbHelper(this);
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db.deleteTable();
}
});
}
}
我的DbHelper代码
public class DbHelper extends SQLiteOpenHelper {
private static String DB_NAME = "QuizDb14.db";
private static String DB_PATH = "";
private SQLiteDatabase mDataBase;
private Context mContext = null;
public DbHelper(Context context) {
super(context, DB_NAME, null, 1);
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
File file = new File(DB_PATH+"QuizDb14.db");
if(file.exists())
openDataBase(); // Add this line to fix db.insert can't insert values
this.mContext = context;
}
public void openDataBase() {
String myPath = DB_PATH + DB_NAME;
mDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
public void copyDataBase() throws IOException {
try {
InputStream myInput = mContext.getAssets().open(DB_NAME);
String outputFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outputFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0)
myOutput.write(buffer, 0, length);
myOutput.flush();
myOutput.close();
myInput.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean checkDataBase() {
SQLiteDatabase tempDB = null;
try {
String myPath = DB_PATH + DB_NAME;
tempDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
e.printStackTrace();
}
if (tempDB != null)
tempDB.close();
return tempDB != null ? true : false;
}
public void createDataBase() throws IOException {
boolean isDBExists = checkDataBase();
if (isDBExists) {
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void insertScore(int score) {
openDataBase();
String query = "INSERT INTO Rank(Score) VALUES(" + score + ")";
mDataBase.execSQL(query);
mDataBase.close();
}
public List<Rank> getRanking() {
List<Rank> listRanking = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM Rank Order By Score DESC LIMIT 10;", null);
try {
if (c == null) return null;
c.moveToNext();
do {
int Id = c.getInt(c.getColumnIndex("ID"));
int Score = c.getInt(c.getColumnIndex("Score"));
Rank ranking = new Rank(Id, Score);
listRanking.add(ranking);
} while (c.moveToNext());
} catch (Exception e) {
e.printStackTrace();
}
finally {
c.close();
db.close();
}
return listRanking;
}
public void deleteTable() {
SQLiteDatabase db = this.getWritableDatabase(); // helper is object extends SQLiteOpenHelper
db.execSQL("DELETE FROM Rank");
db.close();
}
答案 0 :(得分:-1)
您需要使用一些数据库调试工具来查找确切的问题。 Stetho是来自facebook的优秀图书馆,可以帮助您在运行时查看数据库。您可以按照此处的步骤实现它 - http://facebook.github.io/stetho/
让我们知道您是否面临实施此问题的挑战。