我想问一下将Android数据存储到SQLite数据库(我是SQLite数据库的新手)。我一直在尝试将数据存储到SQLite数据库,但是当我用SQLiteStudio看到数据库时,数据是空的。 这是我的代码:
DatabaseHelper.java
package com.example.toolsmanager;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "btm.db";
private static final int DATABASE_VERSION = 1;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public static final String TABLE_1 = "oneroundsingle";
public static final String TABLE1_COLUMN1 = "_ID";
public static final String TABLE1_COLUMN2 = "GAMETYPE";
public static final String TABLE1_COLUMN3 = "PLAYER1";
public static final String TABLE1_COLUMN4 = "PLAYER2";
public static final String TABLE1_COLUMN5 = "SCORE";
public static final String TABLE1_COLUMN6 = "WINNER";
public static final String CREATE_TABLE_ORS = "create table" + TABLE_1 + " ( " +
TABLE1_COLUMN1 + " integer primary key autoincrement, " +
TABLE1_COLUMN2 + " text, " +
TABLE1_COLUMN3 + " text, " +
TABLE1_COLUMN4 + " text, " +
TABLE1_COLUMN5 + " text, " +
TABLE1_COLUMN6 + " text " + ");";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_ORS);
db.execSQL(CREATE_TABLE_ORD);
db.execSQL(CREATE_TABLE_RS);
db.execSQL(CREATE_TABLE_RD);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists" + TABLE_1);
db.execSQL("drop table if exists" + TABLE_2);
db.execSQL("drop table if exists" + TABLE_3);
db.execSQL("drop table if exists" + TABLE_4);
onCreate(db);
}
}
OR_SingleCount.java
package com.example.toolsmanager;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class OR_SingleCount extends AppCompatActivity {
int player1_score, player2_score, final_score;
TextView text_player1_name, text_player2_name;
String score, winner;
DatabaseHelper databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_or_single_count);
text_player1_name = (TextView)findViewById(R.id.player1_name);
text_player1_name.setText(getIntent().getExtras().getString("player1"));
text_player2_name = (TextView)findViewById(R.id.player2_name);
text_player2_name.setText(getIntent().getExtras().getString("player2"));
}
public void singleCount(View v) {
int id = v.getId();
if (id == R.id.player1_plus) {
player1_score += 1;
TextView text_player1_score = (TextView) findViewById(R.id.player1_score);
text_player1_score.setText(String.valueOf(player1_score));
} else if (id == R.id.player1_minus && player1_score != 0) {
player1_score -= 1;
TextView text_player1_score = (TextView) findViewById(R.id.player1_score);
text_player1_score.setText(String.valueOf(player1_score));
} else if (id == R.id.player2_plus) {
player2_score += 1;
TextView text_player2_score = (TextView) findViewById(R.id.player2_score);
text_player2_score.setText(String.valueOf(player2_score));
} else if (id == R.id.player2_minus && player2_score != 0) {
player2_score -= 1;
TextView text_player2_score = (TextView) findViewById(R.id.player2_score);
text_player2_score.setText(String.valueOf(player2_score));
}
finalScore();
}
public void finalScore(){
if ((player1_score == 21 && player2_score < 20) || (player2_score == 21 && player1_score < 20)) {
final_score = 21;
getWinner();
} else if (player1_score == 30 || player2_score == 30) {
final_score = 30;
getWinner();
} else if (player1_score >= 20 && player2_score >= 20) {
if (player1_score == player2_score + 2) {
final_score = player1_score;
getWinner();
} else if (player2_score == player1_score + 2) {
final_score = player2_score;
getWinner();
}
}
}
public void getWinner() {
if (player1_score == final_score){
winner = text_player1_name.getText().toString();
String print_winner = winner + " is the winner";
Toast.makeText(getApplicationContext(),print_winner, Toast.LENGTH_LONG).show();
} else if(player2_score == final_score) {
winner = text_player2_name.getText().toString();
String print_winner = winner + " is the winner";
Toast.makeText(getApplicationContext(), print_winner, Toast.LENGTH_LONG).show();
}
score = player1_score + " - " + player2_score;
String gametype = "Single";
addORSingleData(gametype,
text_player1_name.getText().toString(),
text_player2_name.getText().toString(),
score,
winner);
AlertDialog repeatdialog= new AlertDialog.Builder(this)
.setTitle("Game Finish")
.setMessage("Do you want to repeat the game?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
player1_score = 0;
player2_score = 0;
recreate();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(OR_SingleCount.this, OneRoundMatch.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
}).create();
repeatdialog.show();
}
public void addORSingleData(String gametype, String player1, String player2, String score, String winner){
databaseHelper = new DatabaseHelper(this);
SQLiteDatabase db = databaseHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.TABLE1_COLUMN2, gametype);
contentValues.put(DatabaseHelper.TABLE1_COLUMN3, player1);
contentValues.put(DatabaseHelper.TABLE1_COLUMN4, player2);
contentValues.put(DatabaseHelper.TABLE1_COLUMN5, score);
contentValues.put(DatabaseHelper.TABLE1_COLUMN6, winner);
db.insert(DatabaseHelper.TABLE_1, null, contentValues);
}
}
我的代码出了什么问题?
答案 0 :(得分:-1)
我从未使用过SQLite Studio,因此如果我愿意,我可以解决问题,但你可以设置光标:
Cursor cursor = db.query(TABLE_NAME,null,null,null,null,null,null);
然后致电
Toast.makeText(this, "" + cursor.getCount(), Toast.LENGTH_SHORT).show();
查看您是否正在插入任何数据。另外,onCreate方法中的额外表格是什么?