简体   繁体   English

通过SQLite Manager更新SQLite数据库列之后,为什么更改未反映在Android App中?

[英]After updating my SQLite database columns via SQLite Manager, why are the changes not reflected in my Android App?

I am writing an android app which uses an SQLite database to store information (id, title, author, and rating) about books. 我正在编写一个使用SQLite数据库存储有关书籍的信息(id,标题,作者和等级)的android应用。 Eventually, the goal is to output that information to the user via CRUD implementation. 最终,目标是通过CRUD实现将该信息输出给用户。 Right now, I have a class to establish the various attributes of the Books, an SQLHelper class, a ListAdapter for the eventual listview, and of course the MainActivity class to run the app and implement everything. 现在,我有一个用于建立Books各种属性的类,一个SQLHelper类,一个用于最终listview的ListAdapter,当然还有MainActivity类,用于运行该应用程序并实现所有功能。

I am hard-coding entries for the first 3 columns (id, title, and author) into the DB. 我正在将前3列(id,标题和作者)的条目硬编码到数据库中。 After that, I'm using the Firefox extension SQLite Manager to update the "ratings" column. 之后,我使用Firefox扩展SQLite Manager来更新“评级”列。 Right now however, I cannot get past an error that I have been getting; 但是,现在,我无法克服我一直遇到的错误。 the app crashes when I attempt to run it and it does not appear that the changes I made in the SQLite Manager are reflected in the Logcat file (I export the DB file with , edit with the manager, then import back into the project). 当我尝试运行该应用程序时,该应用程序崩溃了,而且我在SQLite Manager中所做的更改似乎没有反映在Logcat文件中(我先导出了DB文件,然后使用Manager进行编辑,然后又导入到项目中)。

I can and will work on the presentation of the app, UI, and other aesthetic factors at a later time, right now I would just like to figure out why my database changes aren't being reflected. 我可以并且以后会继续研究应用程序,UI和其他美观因素,现在我想弄清楚为什么我的数据库更改没有得到反映。

The full Logcat output is here , but I believe the pertinent information is this: Logcat的完整输出在这里 ,但我相信相关信息是这样的:

11-16 19:32:07.777 2433-2433/? E/AndroidRuntime: FATAL EXCEPTION: main
11-16 19:32:07.777 2433-2433/? E/AndroidRuntime: Process: com.bookreviews.hernandez.bookreviews, PID: 2433
11-16 19:32:07.777 2433-2433/? E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference

From this I gather that the error lies somewhere in my MainActivity.java, but I have yet to determine the cause. 从中我可以收集到该错误位于MainActivity.java中的某个位置,但我尚未确定原因。


MainActivity.java MainActivity.java

package com.bookreviews.hernandez.bookreviews;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.d("Name", "C H");

    SqlHelper db = new SqlHelper(this);

/** CRUD Operations **/
// add Books
    /*
    db.addBook(new Book("Professional Android 4 Application Development",
    "Reto Meier"));
    db.addBook(new Book("Beginning Android 4 Application Development", "Wei-Meng Lee"));
    db.addBook(new Book("Programming Android", "Wallace Jackson"));
    db.addBook(new Book("Hello, Android", "Wallace Jackson"));
    */
// get all books
    List<Book> list = db.getAllBooks();
// update one book
    int j = db.updateBook(list.get(0), "Hello, Android", "Ben Wallace");
// delete one book
// db.deleteBook(list.get(0));
// get all books
    db.getAllBooks();
    ListView listContent = (ListView) findViewById(R.id.list);
    List<Book> books = new ArrayList<Book>();
    books=db.getAllBooks();
//get data from the table by the ListAdapter
    ListAdapter customAdapter = new ListAdapter(this, R.layout.itemlistrow, books );
    listContent.setAdapter(customAdapter);
// get # of records
    db.getIds(list.get(0));
}

@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_main, 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);
}
}

Book.java Book.java

package com.bookreviews.hernandez.bookreviews;

public class Book {
private int id;
private String title;
private String author;
private String rating;
public void setRating(String rating) {
    this.rating = rating;
}
public String getRating() {
    return rating;
}
public Book(){}
public Book(String title, String author) {
    super();
    this.title = title;
    this.author = author;
}
//getters & setters
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public String getAuthor() {
    return author;
}
public void setAuthor(String author) {
    this.author = author;
}
@Override
public String toString() {
    return "Book [id=" + id + ", title=" + title + ", author=" + author
            + ", rating=" + rating + "]";
}
}

SqlHelper.java SqlHelper.java

package com.bookreviews.hernandez.bookreviews;

import java.util.LinkedList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SqlHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 6;
// Database Name
private static final String DATABASE_NAME = "BookDB";
// Books table name
private static final String TABLE_BOOKS = "books";

// Books Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_TITLE = "title";
private static final String KEY_AUTHOR = "author";
public SqlHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// SQL statement to create book table
    String CREATE_BOOK_TABLE = "CREATE TABLE books ( " +
            "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "title TEXT, "+
            "author TEXT, "+
            "rating TEXT)";
    // create books table
    db.execSQL(CREATE_BOOK_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older books table if existed
    db.execSQL("DROP TABLE IF EXISTS books");
// create fresh books table
    this.onCreate(db);
}
/*CRUD operations (create "add", read "get", update, delete) */
public void addBook(Book book){
    Log.d("addBook", book.toString());
    // 1. get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();
    // 2. create ContentValues to add key "column"/value
    ContentValues values = new ContentValues();
    values.put(KEY_TITLE, book.getTitle()); // get title
    values.put(KEY_AUTHOR, book.getAuthor()); // get author
    // 3. insert
    db.insert(TABLE_BOOKS, // table
            null, //nullColumnHack
            values); // key/value -> keys = column names/values
    // 4. Close dbase
    db.close();
}
// Get All Books
public List<Book> getAllBooks() {
    List<Book> books = new LinkedList<Book>();
// 1. build the query
    String query = "SELECT * FROM " + TABLE_BOOKS;
// 2. get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    // 3. go over each row, build book and add it to list
    Book book = null;
    if (cursor.moveToFirst()) {
        do {
            book = new Book();
            book.setId(Integer.parseInt(cursor.getString(0)));
            book.setTitle(cursor.getString(1));
            book.setAuthor(cursor.getString(2));
// Add book to books
            books.add(book);
        } while (cursor.moveToNext());
    }
    Log.d("getAllBooks()", books.toString());
    return books; // return books
}
// Updating single book
public int updateBook(Book book, String newTitle, String newAuthor) {
// 1. get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
    ContentValues values = new ContentValues();
    values.put("title", newTitle); // get title
    values.put("author", newAuthor); // get author
// 3. updating row
    int i = db.update(TABLE_BOOKS, //table
            values, // column/value
            KEY_ID+" = ?", // selections
            new String[] { String.valueOf(159) }); //selection args
// 4. close dbase
    db.close();
    Log.d("UpdateBook", book.toString());
    return i;
}
// Deleting single book
public void deleteBook(Book book) {
// 1. get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();
// 2. delete
    db.delete(TABLE_BOOKS,
            KEY_ID+" = ?",
            new String[] { String.valueOf(book.getId()) });
    // 3. close
    db.close();
    Log.d("deleteBook", book.toString());
}
public int getIds(Book book)
{
    String selectQuery = "SELECT id FROM books";
    SQLiteDatabase database = this.getReadableDatabase();
    Cursor c = database.rawQuery(selectQuery, null);
    c.moveToFirst();
    int total = c.getCount();
    Log.d("getIds", "Count=" + total);

    return total;

}
}

ListAdapter.java ListAdapter.java

package com.bookreviews.hernandez.bookreviews;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

public class ListAdapter extends ArrayAdapter<Book> {
private List<Book> items;
public ListAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
}
public ListAdapter(Context context, int resource, List<Book> items) {
    super(context, resource, items);
    this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi;
        vi = LayoutInflater.from(getContext());
        v = vi.inflate(R.layout.itemlistrow, null);
    }
    Book p = getItem(position);
    if (p != null) {
        TextView tt = (TextView) v.findViewById(R.id._id);
        TextView tt1 = (TextView) v.findViewById(R.id.title);
        TextView tt3 = (TextView) v.findViewById(R.id.author);
        RatingBar rb = (RatingBar) v.findViewById(R.id.rating);
        if (tt != null) {
            tt.setText("" + p.getId());
        }
        if (tt1 != null) {
            tt1.setText(p.getTitle());
        }
        if (tt3 != null) {
            tt3.setText(p.getAuthor());
        }
        if (rb != null) {
            float rating = Float.parseFloat(p.getRating());
            rb.setRating(rating);
        }
    }
    return v;
}
}

itemlistrow.xml itemlistrow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:textColor="#000"
    android:id="@+id/_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="id" android:textStyle="bold"
    android:gravity="left"
    android:layout_weight="1"
    android:typeface="monospace"
    android:height="40sp" />
<TextView android:textColor="#000"
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="title"
    android:layout_weight="1"
    android:height="20sp" />
<TextView android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_weight="1"
    android:textColor="#000"
    android:gravity="right"
    android:id="@+id/author"
    android:text="author"
    android:height="20sp" />
<RatingBar
    android:id="@+id/rating"
    style="?android:attr/ratingBarStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="6dip"
    android:stepSize="0.25"
    android:numStars="5"
    />
</LinearLayout>

activity_main.xml activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="vertical" >
<ListView
    android:id="@+id/list"
    android:layout_width="250dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_height="fill_parent"
    android:layout_gravity="center" >
</ListView>
</LinearLayout>

The Logcat showing DB entries. 显示数据库条目的Logcat。 The ratings still show as "null" despite my having updated them through the SQLite Manager. 尽管我已经通过SQLite Manager更新了等级,但等级仍然显示为“空”。

19:32:07.426 2433-2433/? D/getAllBooks(): [Book [id=1, title=Professional Android 4 Application Development, author=Reto Meier, rating=null], Book [id=2, title=Beginning Android 4 Application Development, author=Wei-Meng Lee, rating=null], Book [id=3, title=Programming Android, author=Wallace Jackson, rating=null], Book [id=4, title=Hello, Android, author=Wallace Jackson, rating=null]]

The Problem: 问题:

My SQLite Manager changes are not reflected in in the Logcat and the app crashes 我的SQLite Manager更改未反映在Logcat中,并且应用程序崩溃

What I've already tried: 我已经尝试过的:

Uninstalling and re-installing the app, upgrading the DB version, deleting the database and starting over 卸载并重新安装该应用程序,升级数据库版本,删除数据库并重新开始

What I'm using: 我正在使用什么:

The latest version of Android Studio 最新版本的Android Studio

Firefox's SQLite Manager Firefox的SQLite Manager

Any suggestions or advice on how to resolve these errors and reflect the database changes would be greatly appreciated. 任何有关如何解决这些错误并反映数据库更改的建议都将不胜感激。

好像您没有在SQLiteHelper类的getAllBooks()方法中SQLiteHelper等级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLite +可序列化,SQLite + GSON,还是仅适用于我的Android应用程序的普通SQLite? - SQLite + Serializable, SQLite + GSON, or just plain SQLite for my Android app? 为什么我的 SQLite select 查询在应用程序中不起作用,但在 Android Studio 上的数据库检查器上起作用? - Why is my SQLite select query not working in app but working on Database Inspector on Android Studio? 为什么在此 SQLite 数据库查询之后我的 Cursor 为空? - Why is my Cursor empty after this SQLite database query? 我的android应用程序的数据库SQLite的排序结果有问题 - I have a Problem with the sorting result of the database SQLite of my android app 为什么我的数据不插入到 android 上的 SQLite 数据库中? - Why don't my data insert into SQLite database on android? 在OnClick更改SQLite数据库中的值后,使用CursorAdapter更新ListView吗? - Updating ListView with CursorAdapter after an OnClick changes a value in SQLite database? 将我的应用程序连接到 SQLite 数据库 - 如何? - Connecting my app to a SQLite database - how to? 无法在我的应用程序中删除SQLite数据库中的行 - Unable to delete a row in SQLite database in my app 为什么我的sqlite数据库没有创建? - Why isn't my sqlite database created? Android Studio 无法读取我的 sqlite 数据库 - Android studio cannot read my sqlite database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM