Java 在Android中更新数据库
我正在通过下面给出的函数更新Sqlite数据库中的一条记录。它们只有1个表和若干列。最初我更新3列。我面临的问题是: 1) 问题是在Java 在Android中更新数据库,java,android,database,sqlite,Java,Android,Database,Sqlite,我正在通过下面给出的函数更新Sqlite数据库中的一条记录。它们只有1个表和若干列。最初我更新3列。我面临的问题是: 1) 问题是在Logcat中显示初始数据,但不显示更新的数据。同时Logcat未显示任何错误 2) 当我在Toast中显示时,只显示我在数据库中静态添加的最后一个值。头两个不出现在Toast中 import android.content.ContentValues; import android.content.Context; import android.database.
Logcat
中显示初始数据,但不显示更新的数据。同时Logcat
未显示任何错误
2) 当我在Toast
中显示时,只显示我在数据库中静态添加的最后一个值。头两个不出现在Toast中
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 ContractSqlite extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "thorcontract_Db";
// TABLE NAME
private static final String TABLE_NAME = "contract";
// COLUMN
private static final String PRIMARY_COLUMN = "heading";
private static final String CONTRACT_TITLES_TITLE = "cnttitle";
private static final String CONTRACT_TITLES_SUBTITLE = "cntSUBTITLE";
private static final String CONTRACT_TITLES_BIDID = "cntbidid";
private static final String LOADSUMMARY_HEADER = "lsheader";
private static final String LOADSUMMARY_FOOTER = "lsfooter";
private static final String SECTION1_TITLE = "s1title";
private static final String SECTION1_BODY = "s1body";
private static final String SECTION2_TITLE = "s2title";
private static final String SECTION2_BODY = "s2body";
private static final String SECTION3_TITLE = "s3title";
private static final String SECTION3_BODY = "s3body";
private static final String SECTION4_TITLE = "s4title";
private static final String SECTION4_BODY = "s4body";
private static final String SECTION5_TITLE = "s5title";
private static final String SECTION5_BODY = "s5body";
private static final String SECTION6_TITLE = "s6title";
private static final String SECTION6_BODY = "s6body";
private static final String SECTION7_TITLE = "s7title";
private static final String SECTION7_BODY = "s7body";
private static final String SECTION8_TITLE = "s8title";
private static final String SECTION8_BODY = "s8body";
private static final String SECTION9_TITLE = "s9title";
private static final String SECTION9_BODY = "s9body";
private String title = null, subtitle = null, bidid = null;
public ContractSqlite(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_CONTRACT_TABLE = " CREATE TABLE " + TABLE_NAME + "("
+ PRIMARY_COLUMN + " INTEGER PRIMARY KEY,"
+ CONTRACT_TITLES_TITLE + " TEXT ," + CONTRACT_TITLES_SUBTITLE
+ " TEXT ," + CONTRACT_TITLES_BIDID + " TEXT,"
+ LOADSUMMARY_HEADER + " TEXT," + LOADSUMMARY_FOOTER + " TEXT,"
+ SECTION1_TITLE + " TEXT," + SECTION1_BODY + " TEXT,"
+ SECTION2_TITLE + " TEXT," + SECTION2_BODY + " TEXT,"
+ SECTION3_TITLE + " TEXT," + SECTION3_BODY + " TEXT,"
+ SECTION4_TITLE + " TEXT," + SECTION4_BODY + " TEXT,"
+ SECTION5_TITLE + " TEXT," + SECTION5_BODY + " TEXT,"
+ SECTION6_TITLE + " TEXT," + SECTION6_BODY + " TEXT,"
+ SECTION7_TITLE + " TEXT," + SECTION7_BODY + " TEXT,"
+ SECTION8_TITLE + " TEXT," + SECTION8_BODY + " TEXT,"
+ SECTION9_TITLE + " TEXT," + SECTION9_BODY + " TEXT" + ")";
db.execSQL(CREATE_CONTRACT_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE" + TABLE_NAME);
onCreate(db);
}
public void addRecord(String title, String subtitle, String bidid) {
SQLiteDatabase db = this.getWritableDatabase();
// String title1 = title;
// String subtitle1 = subtitle;
// String bidid1 = bidid;
ContentValues values = new ContentValues();
try {
values.put(CONTRACT_TITLES_TITLE, title);
values.put(CONTRACT_TITLES_SUBTITLE, subtitle);
values.put(CONTRACT_TITLES_BIDID, bidid);
db.insert(TABLE_NAME, null, values);
Log.i("Initial Data", " " + title + " " + subtitle + " " + bidid);
db.close();
} catch (Exception e) {
db.close();
}
}
public String[] getrecord() {
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor != null && cursor.moveToFirst()) {
title = cursor.getString(cursor
.getColumnIndex(CONTRACT_TITLES_TITLE));
subtitle = cursor.getString(cursor
.getColumnIndex(CONTRACT_TITLES_SUBTITLE));
bidid = cursor.getString(cursor
.getColumnIndex(CONTRACT_TITLES_BIDID));
// Dumps "Title: Test Title Content: Test Content"
Log.i("CONTRACTTITLES", "Title: " + title + " Content: " + subtitle);
cursor.close();
}
return new String[] { title, subtitle, bidid };
}
public int update(String title, String subtitle, String id) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(CONTRACT_TITLES_TITLE, title);
values.put(CONTRACT_TITLES_SUBTITLE, subtitle);
values.put(CONTRACT_TITLES_SUBTITLE, id);
return db.update(TABLE_NAME, values, PRIMARY_COLUMN + " =?",
new String[] { String.valueOf(1) });
}
}
方法从主活动调用
csq是数据库类对象
用于添加记录
csq.addRecord("abc", "xyz", "1234");
String[] str = csq.getrecord();
Toast.makeText(getActivity(),
" " + str[0] + " " + str[1] + "" + str[2], Toast.LENGTH_LONG).show();
更新
csq.update(cnttitle_str, cntsubtitle_str, cntbid_str);
为了得到一张唱片
csq.addRecord("abc", "xyz", "1234");
String[] str = csq.getrecord();
Toast.makeText(getActivity(),
" " + str[0] + " " + str[1] + "" + str[2], Toast.LENGTH_LONG).show();
更新的数据没有显示,因为您没有
日志
行(如果我理解正确的话)
您只得到一行,因为您需要遍历光标
并返回一个列表或类似列表。此选项是:
如果要查看所有记录,请在数据库类中执行以下操作:
public void showAllDBcells() {
String selectQuery = "SELECT * FROM " + TABLE_DB;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Log.d("your tag",cursor.getString(0));
Log.d("your tag",cursor.getString(1));
Log.d("your tag",cursor.getString(2));
// and for another data , just extends code for 3 4 5 ... columns
} while (cursor.moveToNext());
}
//
cursor.close();
db.close();
}
您可以在活动中调用方法,并在Logcat中查看数据库中存储的数据。请检查我的答案,如果这有用,请投赞成票