调用另一个类失败了。我的数据库没有显示它的数据

时间:2013-02-04 09:43:37

标签: android database sqlite

我被卡住了,我不知道这是什么问题。我的数据库没有显示它的数据。请帮助找到问题。

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class highscore {
     private static final String DATABASE_NAME = "topscoredb";
     private static final String DATABASE_TABLE = "tablescore";
     private static final int DATABASE_VERSION = 1;
     public static final String KEY_ROWID = "_id";
     public static final String KEY_SCORE = "score";
     public static final String KEY_NAME = "players_name";

     private DbHelper ourHelper;
     private final Context ourContext;
     private SQLiteDatabase ourDatabase;

     private static class DbHelper extends SQLiteOpenHelper{
        public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

            @Override
            public void onCreate(SQLiteDatabase db) {
                // TODO Auto-generated method stub

            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                         KEY_ROWID + "INTEGER PRIMARY INCREMENT, " +
                         KEY_NAME + "TEXT NOT TO NULL, " +
                         KEY_SCORE + "TEXT NOT TO NULL);"

                    );

            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion,
                    int newVersion) {
                // TODO Auto-generated method stub
                db.execSQL("DROP TABLE IF EXIST" + DATABASE_TABLE);
                onCreate(db);

            }
          }

         public highscore(Context c){
      ourContext = c;
         }

           public highscore open() throws SQLException{
     ourHelper = new DbHelper(ourContext);
     ourDatabase = ourHelper.getWritableDatabase();


      return this;

                     }

                   public void close(){
       ourHelper.close();

            }



              public long createEntry(String name, String tscore) {
          // TODO Auto-generated method stub

                   ContentValues cv = new ContentValues();
                   cv.put(KEY_NAME, name);
                   cv.put(KEY_SCORE, tscore);
              return  ourDatabase .insert(DATABASE_TABLE, null, cv);
       }

              public String getData() {
          String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_SCORE};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,null,null);
    String result = "";

    int irow = c.getColumnIndex(KEY_ROWID);
    int iname = c.getColumnIndex(KEY_NAME);
    int iscore = c.getColumnIndex(KEY_SCORE);

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        result = result + c.getString(irow)+ "   " + c.getString(iname) +
                 "   "+ c.getString(iscore) + "\n";
    }

    return result;
        }

       }


heres some code


         public class Viewhs extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(R.layout.topscores);

        TextView tv = (TextView) findViewById(R.id.highscoredata);
        highscore info = new highscore(this);
        info.open();
        String data = info.getData();
        info.close();
        tv.setText(data);

    }

}



this my method to insert data to database. my scored is an integer and my dataname is already a string


            public void getdata(){


            try {
            String name = dataname;
            String tscore = scored.getText().toString();

            highscore entry = new highscore(Playing.this);
            entry.open();
            entry.createEntry(name, tscore);
            entry.close();
            }catch (Exception e) {

            }   

               }        

1 个答案:

答案 0 :(得分:0)

你必须按照这个例子创建表

               private final String SQL = " CREATE TABLE IF NOT EXISTS " + "table name" + " (" 
          + "_id" + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
          + "name" + " TEXT NOT NULL," 
          + "age" + " INTEGER NOT NULL);";