这个应用程序可以运行,但无法打印数据库中的数据, 我也不知道数据是否能够插入。
这是MainActivity.java
package com.zikri.sqlitetuto3;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText dataET;
Button addB, delB;
TextView dataTV;
MyDBHandler dbHandler;
String getProduct;
Products products;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startAndroidApp();
}
public void startAndroidApp(){
dbHandler = new MyDBHandler(this,null,null,1);
dataET = (EditText) findViewById(R.id.dataET);
addB = (Button) findViewById(R.id.addB);
delB = (Button) findViewById(R.id.delB);
dataTV = (TextView) findViewById(R.id.dataTV);
getProduct = dataET.getText().toString();
PrintProduct();
addB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AddProduct(getProduct);
}
});
delB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DelProduct(getProduct);
}
});
}
public void PrintProduct(){
dataTV.setText(dbHandler.PrintProduct());
dataET.setText("");
}
public void AddProduct(String p){
products = new Products(p);
dbHandler.AddProduct(products);
PrintProduct();
}
public void DelProduct(String p){
dbHandler.DelProduct(p);
PrintProduct();
}
}
这是MyDBHandler.java
package com.zikri.sqlitetuto3;
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 PRODUCT_TABLE = "product";
private static final String COL_ID = "id";
private static final String COL_PRODUCTNAME = "product_name";
public MyDBHandler(Context context,
String name,SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db){
String query = "CREATE TABLE "+PRODUCT_TABLE+" ("+
COL_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
COL_PRODUCTNAME+" TEXT"+");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
String query = "DROP IF EXIST "+PRODUCT_TABLE;
db.execSQL(query);
onCreate(db);
}
public void AddProduct(Products products){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_PRODUCTNAME,products.get_productname());
db.insert(PRODUCT_TABLE,null,values);
db.close();
}
public void DelProduct(String product){
SQLiteDatabase db = getWritableDatabase();
String query = "DELETE FROM "+PRODUCT_TABLE+
"WHERE "+COL_PRODUCTNAME+" = "+product+";";
db.execSQL(query);
db.close();
}
public String PrintProduct(){
SQLiteDatabase db = getWritableDatabase();
String printproductstring = "";
String query = "SELECT * FROM "+PRODUCT_TABLE+";";
Cursor cur = db.rawQuery(query,null);
cur.moveToFirst();
while(cur.moveToNext()){
printproductstring =
printproductstring+cur.getString(cur.getColumnIndex("product_name"));
printproductstring = printproductstring + "\n";
}
db.close();
return printproductstring;
}
}
这是Product.java
package com.zikri.sqlitetuto3;
public class Products {
private int _ID;
private String _productname;
public Products() {}
public Products(String productname) {
this._productname = productname;
}
public void set_ID(int _ID) {
this._ID = _ID;
}
public int get_ID() {
return _ID;
}
public void set_productname(String _productname) {
this._productname = _productname;
}
public String get_productname() {
return _productname;
}
}
这是activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.zikri.sqlitetuto3.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/dataET" />
<Button
android:text="INSERT"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/dataET"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/addB" />
<Button
android:text="Delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/addB"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/delB" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/dataTV" />
答案 0 :(得分:0)
当button
为clicked
时,您应该接受输入,否则getProduct
将一直为空
addB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getProduct = dataET.getText().toString();
// ^^^ fetch value when button is clicked
AddProduct(getProduct);
}
});
并在删除时执行相同操作
delB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getProduct = dataET.getText().toString();
// ^^^ fetch value when button is clicked
DelProduct(getProduct);
}
});
此外,您可以确认return value
long
成功插入 long id = db.insert(PRODUCT_TABLE,null,values);
// you can now check the value of id through Logs or Toast
db.close();
$find_letters = array('a', 'c', 'd');
$string = 'abcdefg';
$match = (str_replace($find_letters, '', $string) != $string);
类型并定义为
新插入行的行ID,如果发生错误,则返回-1
所以使用它
n%3
答案 1 :(得分:0)
很难看到android中的数据库是否被遗忘你是否可以在logcate上看到错误,如果没有错误意味着它存储但不是每次都存在。如果你想要仍然需要在devide manager中看到其难以解决的其他解决方案一个root的移动和intlel root brouser去android文件夹里面的android文件夹再去dada里面去数据然后你的包名然后数据库然后看看有没有任何文件与你的数据库相关的名称复制到pc并下载sqllite brouser。这个漫长的过程,你可以100%的机会获得数据库文件。如果它被创建