2014-10-10 88 views
-1

嗨fokes我正在构建一个android应用程序,我需要做一个sql数据库来节省时间和分数。请帮助Sql问题

我也想知道它被保存了多少次,所以我可以用它来给用户播放时间。

基本上我需要sql来创建10秒和0分。然后在每场比赛之后,我希望有更多的时间可以说10.01秒(或多或少不重要)并保存得分。

现在,当我运行游戏时它可以工作,但是当它必须对sql数据库执行任何操作时,它会崩溃。

这是我的SQL数据库的Java代码:

包com.orion.peky.thetapgame;

import android.content.ContentValues; import android.content.Context; import android.database.Cursor;导入 android.database.sqlite.SQLiteDatabase;导入 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; 

} } 

这里是我的游戏活动:

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); 
     } 
    } 
} 

这里是我的游戏活动后,屏幕:

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(); 

    } 



} 

这里是我的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) 

我还有一件事情我已经研究了SQL数据库,但似乎我找不到问题,所以请不要将此标记为重复项,并且可能存在多个错误。至少感谢阅读。

回答

0

解决了这是结果。

@Override 公共无效的onCreate(SQLiteDatabase分贝){ db.execSQL( “CREATE TABLE” + IME_TABLICE + “(” +
KEY_ROWID + “INTEGER PRIMARY KEY AUTOINCREMENT,” + KEY_TIME +“INTEGER ,“+ KEY_SCORE +”INTEGER“)