Hi fokes I am building an android app and I need to make an sql database to save time and score.
I would also like to get how many times it has been saved so I can use it to give the user times played.
Basically I need the sql to have on create 10 seconds and 0 score. Then after every play I would like it to have more times lets say 10.01 seconds ( more or less it doesnt matter) and to save the score.
Now when I run the game it works but when it has to do anything with the sql database it crashes.
Here is my sql database java code:
package com.orion.peky.thetapgame;
import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper;
public class spremanje { public static final String KEY_ROWID = "_id"; public static final String KEY_SCORE = "score"; public static final String KEY_TIME = "time";
private static final String IME_TABLICE = "bodovi";
private static final String TABELA_BAZE = "nmvz";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, IME_TABLICE, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABELA_BAZE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_TIME + " INTEGER , " +
KEY_SCORE + "INTEGER )"
); ContentValues cv = new ContentValues(); int time=10000;
cv.put(KEY_TIME, time);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABELA_BAZE);
onCreate(db);
}
}
public spremanje(Context c) {
ourContext = c;
}
public spremanje open() {
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
public long createEntry(int time, int score) {
ContentValues cv = new ContentValues();
cv.put(KEY_TIME, time);
cv.put(KEY_SCORE, score);
return ourDatabase.insert(IME_TABLICE, null, cv);
}
public int getRed() {
String[] columns = new String[]{KEY_ROWID, KEY_TIME, KEY_SCORE};
Cursor c = ourDatabase.query(IME_TABLICE, columns, null, null, null, null, null);
int red = 0;
int iRow = c.getColumnIndex(KEY_ROWID);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
red = c.getInt(iRow);
}
return red;
}
public int getTime() {
String[] columns = new String[]{KEY_ROWID, KEY_TIME, KEY_SCORE};
Cursor c = ourDatabase.query(IME_TABLICE, columns, null, null, null, null, null);
int time = 10000;
int iTime = c.getColumnIndex(KEY_TIME);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
time = c.getInt(iTime);
}
return time;
}
public int getScore() {
String[] columns = new String[]{KEY_ROWID, KEY_TIME, KEY_SCORE};
Cursor c = ourDatabase.query(IME_TABLICE, columns, null, null, null, null, null);
int score = 0;
int iScore = c.getColumnIndex(KEY_SCORE);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
score = c.getInt(iScore);
}
return score;
} }
Here is my game activity:
package com.orion.peky.thetapgame;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.TextView;
import com.google.android.gms.ads.*;
public class Game extends Activity {
TextView tekst, vrijeme;
int brojac=0, provjera=0;
CountDownTimer Count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
spremanje read = new spremanje(this);
read.open();
int brojacvremena=read.getTime();
read.close();
tekst=(TextView)findViewById(R.id.tekst);
vrijeme=(TextView)findViewById(R.id.vrijeme);
final Intent i =new Intent(Game.this,Score.class);
vrijeme.setText("Time: "+brojacvremena/1000 + "." + brojacvremena%1000);
/*AdView adView=(AdView)this.findViewById(R.id.adView123);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);*/
Count = new CountDownTimer(brojacvremena, 1) {
public void onTick(long millisUntilFinished) {
int seconds = (int) ((millisUntilFinished / 1000));
vrijeme.setText(seconds + "." + millisUntilFinished % 1000);
}
public void onFinish(){
i.putExtra("prijenos",brojac);
startActivity(i);
finish();
}};
}
public void broji(View view){
if(provjera==0){
Count.start();
brojac++;
provjera++;
tekst.setText("Taps " + brojac);
}else {
brojac++;
tekst.setText("Taps " + brojac);
}
}
}
Here is the screen after my game activity:
package com.orion.peky.thetapgame;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Score extends Activity {
int primljeno=1, vrijeme=10000;
Bundle dodatak;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_score);
TextView prikazati=(TextView)findViewById(R.id.prikazbodova);
Intent inte=getIntent();
dodatak=inte.getExtras();
primljeno=dodatak.getInt("prijenos");
prikazati.setText("You tapped "+primljeno+" times");
spremanje unos = new spremanje(this);
unos.open();
vrijeme= unos.getTime();
vrijeme=vrijeme+125;
unos.createEntry(vrijeme,primljeno);
unos.close();
}
}
and here is my logcat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.orion.peky.thetapgame/com.orion.peky.thetapgame.Game}: android.database.sqlite.SQLiteException: no such table: bodovi: , while compiling: SELECT _id, time, score FROM bodovi
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: no such table: bodovi: , while compiling: SELECT _id, time, score FROM bodovi
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1449)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1485)
at com.orion.peky.thetapgame.spremanje.getTime(spremanje.java:87)
at com.orion.peky.thetapgame.Game.onCreate(Game.java:22)
at android.app.Activity.performCreate(Activity.java:4465)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
One more thing i have already researched the sql database but it seems i can not find the problem so please don't mark this as a duplicate and there might be more than 1 mistake. Thanks for reading at least.
0 comments:
Post a Comment