Java Android数据未存储到SQLite数据库
我想问一些关于将Android数据存储到SQLite数据库的问题(我是SQLite数据库的新手)。我一直在尝试将数据存储到SQLite数据库,但当我看到带有SQLiteStudio的数据库时,数据是空的。 这是我的代码: DatabaseHelper.javaJava Android数据未存储到SQLite数据库,java,android,database,sqlite,Java,Android,Database,Sqlite,我想问一些关于将Android数据存储到SQLite数据库的问题(我是SQLite数据库的新手)。我一直在尝试将数据存储到SQLite数据库,但当我看到带有SQLiteStudio的数据库时,数据是空的。 这是我的代码: DatabaseHelper.java package com.example.toolsmanager; import android.content.Context; import android.database.sqlite.SQLiteDatabas
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);
}
}
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);
}
}
或_SingleCount.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);
}
}
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);
}
}
package com.example.toolsmanager;
导入android.content.ContentValues;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.database.sqlite.SQLiteDatabase;
导入android.support.v7.app.AlertDialog;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.TextView;
导入android.widget.Toast;
公共类或\u SingleCount扩展了AppCompative活动{
国际球员1分、2分、决赛分;
text查看text\u player1\u名称,text\u player2\u名称;
弦乐得分,胜者;
数据库助手数据库助手;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u或\u single\u 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”);
}
公共void singleCount(视图五){
int id=v.getId();
if(id==R.id.player1\u加){
球员1分(u分)+=1分;;
TextView text_player1_分数=(TextView)findViewById(R.id.player1_分数);
text_player1_score.setText(String.valueOf(player1_score));
}否则如果(id==R.id.player1\u减去&player1\u分数!=0){
球员1分-=1分;
TextView text_player1_分数=(TextView)findViewById(R.id.player1_分数);
text_player1_score.setText(String.valueOf(player1_score));
}else if(id==R.id.player2\u plus){
球员2分+1分;
TextView text_player2_分数=(TextView)findViewById(R.id.player2_分数);
text_player2_score.setText(String.valueOf(player2_score));
}否则如果(id==R.id.player2\u减和&player2\u得分!=0){
球员2分(u分)=1分;;
TextView text_player2_分数=(TextView)findViewById(R.id.player2_分数);
text_player2_score.setText(String.valueOf(player2_score));
}
finalScore();
}
public void finalScore(){
如果((player1_得分=21和&player2_得分<20)|(player2_得分=21和&player1_得分<20)){
最终得分=21分;
getWinner();
}否则如果(玩家1|U分数=30 |玩家2|U分数=30){
最终得分=30分;
getWinner();
}否则如果(玩家1分>=20和玩家2分>=20){
如果(玩家1分==玩家2分+2分){
最终得分=选手1分;
getWinner();
}否则如果(玩家2分==玩家1分+2分){
最终得分=运动员2分;
getWinner();
}
}
}
公开无效的getWinner(){
如果(球员1分=最终分){
winner=text\u player1\u name.getText().toString();
String print_winner=winner+“是赢家”;
Toast.makeText(getApplicationContext(),print_winner,Toast.LENGTH_LONG).show();
}否则如果(玩家2分=最终分){
winner=text\u player2\u name.getText().toString();
String print_winner=winner+“是赢家”;
Toast.makeText(getApplicationContext(),print_winner,Toast.LENGTH_LONG).show();
}
分数=玩家1分+玩家2分;
字符串gametype=“Single”;
addORSingleData(游戏类型,
text\u player1\u name.getText().toString(),
text\u player2\u name.getText().toString(),
分数
优胜者);
AlertDialog repeatdialog=新建AlertDialog.Builder(此)
.setTitle(“游戏结束”)
.setMessage(“是否要重复游戏?”)
.setPositiveButton(“是”,新的DialogInterface.OnClickListener(){
public void onClick(对话框接口对话框,int whichButton){
球员1分=0分;
球员2分=0分;
重新创建();
}
})
.setNegativeButton(“否”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
Intent i=新Intent(或_SingleCount.this,OneRoundMatch.class);
i、 设置标志(意图、标志、活动、清除、顶部);
星触觉(i);
完成();
}
}).create();
repeatdialog.show();
}
public void addORSingleData(字符串游戏类型、字符串玩家1、字符串玩家2、字符串分数、字符串赢家){
databaseHelper=新的databaseHelper(此);
SQLiteDatabase db=databaseHelper.getWritableDatabase();
ContentValues ContentValues=新ContentValues();
contentValues.put(DatabaseHelper.TABLE1\u COLUMN2,游戏类型);
contentValues.put(DatabaseHelper.TABLE1_COLUMN3,player1);
contentValues.put(DatabaseHelper.TABLE1_COLUMN4,player2);
contentValues.put(DatabaseHelper.TABLE1_COLUMN5,score);
contentValues.put(DatabaseHelper.TABLE1_COLUMN6,winner);
插入(DatabaseHelper.TABLE_1,null,contentValues);
}
}
我的代码出了什么问题?我从未使用过SQLite Studio,因此如果我想解决这个问题,我不会遇到麻烦,但您可以设置一个光标:
Cursor cursor = db.query(TABLE_NAME,null,null,null,null,null,null);
然后打电话
Toast.makeText(this, "" + cursor.getCount(), Toast.LENGTH_SHORT).show();
看看你