我想获取一个输入字符串,然后单击Add it it it how under show,依此类推 但是当我点击"添加"按钮它得到了坚持。而不是在那里展示。
我的MainActivity布局:
MainActivity.java
package com.theaditya.sqlitewala;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText adiInput;
TextView adiText;
MyDbHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adiInput =(EditText) findViewById(R.id.adiInput);
adiText =(TextView)findViewById(R.id.adiText);
dbHandler=new MyDbHandler(this,null,null,1);
printDatabase();
}
public void printDatabase(){
String dbString= dbHandler.databaseToString();
adiText.setText(dbString);
adiInput.setText("");
}
public void addButtonClicked(View view){
Products products= new Products(adiInput.getText().toString());
dbHandler.addProduct(products);
printDatabase();
}
public void deleteButtonClicked(View view){
String inputText =adiInput.getText().toString();
dbHandler.deleteProduct(inputText);
printDatabase();
}
}
MyDbHandler.java
package com.theaditya.sqlitewala;
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="product.db";
private static final String TABLE_PRODUCTS="products"; //its the TABLE NAME
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, DATABASE_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);
}
//for adding new Row to DataBase
public void addProduct(Products products){
ContentValues values=new ContentValues();
values.put(COLUMN_PRODUCTNAME,products.get_productname());
SQLiteDatabase db=getWritableDatabase();
db.insert(TABLE_PRODUCTS,null,values);
db.close();
}
//for deleting Row to DataBase
public void deleteProduct(String productName){
SQLiteDatabase db=getWritableDatabase();
db.execSQL("DELETE FROM "+TABLE_PRODUCTS+" WHERE "+COLUMN_PRODUCTNAME+"=\""+productName+"\";" );
}
public String databaseToString(){
String dbString="";
SQLiteDatabase db=getWritableDatabase();
String query="SELECT * FROM "+TABLE_PRODUCTS+" WHERE 1";
//cursor points to the location of ur result
Cursor c=db.rawQuery(query,null);
//move to the first row in result
c.moveToFirst();
while (!c.isAfterLast()) {
if (c.getString(c.getColumnIndex("productname")) != null) {
dbString += c.getString(c.getColumnIndex("productname"));
dbString += "\n";
}
}
db.close();
return dbString;
}
}
Product.java
package com.theaditya.sqlitewala;
public class Products {
private int _id;
private String _productname;
public Products(String _productname) {
this._productname = _productname;
}
public Products() {
}
public void set_id(int _id) {
this._id = _id;
}
public void set_productname(String _productname) {
this._productname = _productname;
}
public int get_id() {
return _id;
}
public String get_productname() {
return _productname;
}
}
答案 0 :(得分:0)
你的databaseToString()方法查询错误:
如果你想得到以下数据库的第一行将起作用:
public String databaseToString() {
// String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";
String query = "SELECT * FROM " + TABLE_PRODUCTS + " ORDER BY "
+ COLUMN_ID + " DESC LIMIT 1";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
String str = "";
if (cursor.moveToFirst())
str = cursor.getString(cursor.getColumnIndex(COLUMN_PRODUCTNAME));
cursor.close();
return str;
}
从代码中获取db的所有记录将起作用:
public String databaseToString() {
String query = "SELECT * FROM " + TABLE_PRODUCTS ;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
String str = "";
if (cursor.moveToFirst()) {
do {
str = cursor.getString(cursor.getColumnIndex(COLUMN_PRODUCTNAME)) +"\n";
} while (cursor.moveToNext());
}
cursor.close();
return str;
}