I am having trouble trying to implement an SQLite database in my android studio project. I am trying to add a row of user information including name, password, age, gender, and the level they got to in the previous game.
In my database class, which is an extension of the SQLiteOpenHelper, I have:
package com.example.android.assignment4;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.SyncStateContract;
/**
* Created by cristian on 9/21/2015.
*/
public class DataBase extends SQLiteOpenHelper {
private static final int TABLE_VERSION = 3;
private static final String DATABASE_NAME = "database.db";
public static final String TABLE_USERS = "users";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_USERNAME = "name";
public static final String COLUMN_USERPASS = "pass";
public static final String COLUMN_USERAGE = "age";
public static final String COLUMN_LEVEL = "level";
public static final String COLUMN_GENDER = "gender";
private static final String[] columns = {COLUMN_ID, COLUMN_USERNAME, COLUMN_USERPASS, COLUMN_USERAGE, COLUMN_LEVEL, COLUMN_GENDER};
public DataBase(Context context) {
super(context, DATABASE_NAME, null, TABLE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_USERS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_LEVEL + " INTEGER, " +
COLUMN_USERNAME + " TEXT, " +
COLUMN_USERPASS + " TEXT, " +
COLUMN_USERAGE + " TEXT, " +
COLUMN_GENDER + " TEXT " +
");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
onCreate(db);
}
//Add a row of a user instance
public void addUser(User user)
{
ContentValues cv = new ContentValues();
cv.put(COLUMN_LEVEL, user.getLevel());
cv.put(COLUMN_USERNAME,user.getName());
cv.put(COLUMN_USERPASS,user.getPass());
cv.put(COLUMN_USERAGE,user.getAge());
cv.put(COLUMN_GENDER, user.getGender());
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE_USERS, null, cv);
db.close();
}
public String getRowUserData(int i)
{ String s ="";
SQLiteDatabase db = getReadableDatabase();
String query = "SELECT * FROM " + TABLE_USERS + " WHERE 1" + " ORDER BY " + COLUMN_ID + " DESC";
Cursor c = db.query(TABLE_USERS, columns, " id = ?", new String[] { String.valueOf(i) }, null, null, null);
c.moveToFirst();
s += c.getString(0) + " ";
s += c.getString(1) + " ";
s += c.getString(2) + " ";
s += c.getString(3) + " ";
s += c.getString(4) + "\n";
return s;
}
}
In my activity class I have
package com.example.android.assignment4;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import java.util.*;
import java.io.*;
public class page3 extends AppCompatActivity {
DataBase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page3);
db = new DataBase(this);
Button backButton = (Button) findViewById(R.id.Return);
backButton.setOnClickListener(backListener);
LinearLayout l = (LinearLayout) findViewById(R.id.linearLayout);
//Constuctor with level, name, password, age, and gender
User user = new User(3, "Test", "pass", "17", "Male");
db.addUser(user);
//try to access first entry of Users
for (int i = 0; i <= 1; i++) {
String rowData = db.getRowUserData(i);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_page3, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Everytime I run the program, it crashes on the line:
Cursor c = db.query(TABLE_USERS, columns, " id = ?", new String[] { String.valueOf(i) }, null, null, null);
in the GetRowUserData function.
So I am assuming that the data is not being added to the database and any attempts to retrieve the data that is not there will return an exception. Anyone know what I am doing wrong?
Aucun commentaire:
Enregistrer un commentaire