android - 连接sqlite数据库后无法使用android应用程序
问题描述
我正在开发连接 sqlite 数据库的示例 android 应用程序。由于显示错误,我看不到真正的界面。不显示任何东西,有时显示也没有反应。它没有显示任何错误,但 android 设备显示图像我也在下面添加我的代码。Myactivity.java 类
package com.example.randikawann.androidconnectsqlite;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText userInput;
TextView userText;
MyDBHandler myDBHandler;
private static final String TAG = "MyActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userInput = (EditText) findViewById(R.id.user_input);
userText = (TextView) findViewById(R.id.user_text);
myDBHandler=new MyDBHandler(this,null,null,1);
printDatabase();
Log.i(TAG, "start application");
}
public void addButtonClicked(View view){
Products products=new Products(userInput.getText().toString());
myDBHandler.addProduct(products);
printDatabase();
Log.i(TAG, "add button clicked");
}
public void deleteButtonClicked(View view){
String inputText=userInput.getText().toString();
myDBHandler.deleteproduct(inputText);
printDatabase();
Log.i(TAG, "delete button clicked");
}
private void printDatabase() {
String dbString =myDBHandler.databaseToString();
userText.setText(dbString);
userInput.setText("");
Log.i(TAG, "print database");
}
@Override
protected void onDestroy() {
myDBHandler.close();
super.onDestroy();
}
}
这是 MyDBHandler.java 类
package com.example.randikawann.androidconnectsqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDBHandler extends SQLiteOpenHelper{
private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME="products.db";
private static final String TABLE_PRODUCTS="products";
private static final String COLUMN_ID="id";
private static final String COLUMN_PRODUCTNAME="productname";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
super(context, DATABASE_NAME, factory,version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT " +
");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_PRODUCTS);
onCreate(db);
}
//add a new row to database
public void addProduct(Products products){
SQLiteDatabase db=getWritableDatabase();
ContentValues values=new ContentValues();
values.put(COLUMN_PRODUCTNAME,products.get_productname());
db.insert(TABLE_PRODUCTS,null,values);
db.close();
}
//delect a row in database
public void deleteproduct(String productName){
SQLiteDatabase db=getWritableDatabase();
String query="DELETE FROM "+ TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "=\"" + productName +"\";" ;
db.execSQL(query);
}
//printout database as a string
public String databaseToString(){
String dbString = "";
SQLiteDatabase db = this.getWritableDatabase();
String query="SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";
//cursor point to a location in your result
Cursor c=db.rawQuery(query, null);
//move to first row in your results
c.moveToFirst();
while (!c.isAfterLast()){
if (c.getString(c.getColumnIndex("productname"))!=null){
dbString +=c.getString(c.getColumnIndex("productname"));
dbString +="\n";
}
}
db.close();
return dbString;
}
}
Products.java 类是
package com.example.randikawann.androidconnectsqlite;
public class Products {
private int _id;
private String _productname;
public Products() {
}
public Products(String productname) {
this._productname = productname;
}
public int get_id() {
return _id;
}
public String get_productname() {
return _productname;
}
public void set_id(int _id) {
this._id = _id;
}
public void set_productname(String _productname) {
this._productname = _productname;
}
}
我也将 xml 文件调用添加为活动 main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/user_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginStart="150dp"
android:layout_marginTop="250dp"
android:text="Hello World!"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp"
android:layout_marginTop="150dp"
android:onClick="addButtonClicked"
android:text="Add"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/delete_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="150dp"
android:onClick="deleteButtonClicked"
android:text="Delete"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/user_input"
android:layout_width="262dp"
android:layout_height="wrap_content"
android:layout_marginLeft="62dp"
android:layout_marginStart="62dp"
android:layout_marginTop="86dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
解决方案
我认为错误显示在 down 语句中。请告诉我如何解决这个问题
while (!c.isAfterLast()){
if (c.getString(c.getColumnIndex("productname"))!=null){
dbString +=c.getString(c.getColumnIndex("productname"));
dbString +="\n";
}else{
Log.i(TAG,"this is in else");
// }
}
推荐阅读
- django - Django rest api - 使用搜索过滤器搜索方法字段
- web-scraping - Goutte Scraping - JSON-LD - 仅“@type”:“Product”
- python - multiprocessing.Pool.map_async() 的结果是否以与输入相同的顺序返回?
- java - 读取 java 中的 CSV 文件并检索处理所有 n-case 的值
- python - 有没有办法通过在单独的 df 中出现项目(按顺序)来复制和连接 df 组(订单)?
- javascript - 在使用电子邮件作为唯一标识符的同时,限制一个人提交表单超过 X 次的最佳方法是什么?
- php - 只允许唯一的 $_GET 请求?生成、验证、禁止值
- data-binding - 如何在数据绑定中调用函数?
- spring-boot - 当工作线程池变得太大时,带有undertow的Springboot变得无响应
- sql-server - 如何在某些条件下找到一个字段的总和?