嗨,所有我有一个小问题,我是Android的新手,我正在尝试实现我在this网站上找到的sqlite示例......
我的问题是我添加了2列额外的示例我发现应用程序崩溃可以任何一个请告诉我我的错误是什么
这是代码:
DatabaseActivity.java
package net.learn2develop.Database;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Toast;
public class DatabaseActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DBAdapter db = new DBAdapter(this);
//---add 2 titles---
db.open();
long id;
id = db.insertTitle(
"0470285818",
"Hanudi is the best :)",
"Wrox",
"www.link1.com",
"5mi");
id = db.insertTitle(
"047017661X",
"Professional Windows Vista Gadgets Programming",
"Wrox",
"www.link2.com",
"7mi");
db.close();
//---get all titles---
db.open();
Cursor c = db.getAllTitles();
if (c.moveToFirst())
{
do {
DisplayTitle(c);
} while (c.moveToNext());
}
db.close();
}
public void DisplayTitle(Cursor c)
{
Toast.makeText(this,
"id: " + c.getString(0) + "\n" +
"ADNO: " + c.getString(1) + "\n" +
"FROM: " + c.getString(2) + "\n" +
"TO: " + c.getString(3) + "\n" +
"LINK: " + c.getString(4) + "\n" +
"DIST: " + c.getString(5),
Toast.LENGTH_LONG).show();
}
}
这是DBAdapter.java
package net.learn2develop.Database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter
{
public static final String KEY_ROWID = "_id";
public static final String KEY_NUMBER = "number";
public static final String KEY_FROM = "fromtime";
public static final String KEY_TO = "totime";
public static final String KEY_LINK = "link";
public static final String KEY_DIST = "dist";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "books";
private static final String DATABASE_TABLE = "titles";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table titles (_id integer primary key autoincrement, "
+ "number text not null, from text not null, to not null "
+ "link text not null, dist text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS titles");
onCreate(db);
}
}
//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a title into the database---
public long insertTitle(String number, String from, String to, String link, String dist)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NUMBER, number);
initialValues.put(KEY_FROM, from);
initialValues.put(KEY_TO, to);
initialValues.put(KEY_LINK, link);
initialValues.put(KEY_DIST, dist);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---deletes a particular title---
public boolean deleteTitle(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID +
"=" + rowId, null) > 0;
}
//---retrieves all the titles---
public Cursor getAllTitles()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_NUMBER,
KEY_FROM,
KEY_TO,
KEY_LINK,
KEY_DIST},
null,
null,
null,
null,
null,
null);
}
//---retrieves a particular title---
public Cursor getTitle(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_NUMBER,
KEY_FROM,
KEY_TO,
KEY_LINK,
KEY_DIST
},
KEY_ROWID + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//---updates a title---
public boolean updateTitle(long rowId, String number,
String from, String to, String link, String dist)
{
ContentValues args = new ContentValues();
args.put(KEY_NUMBER, number);
args.put(KEY_FROM, from);
args.put(KEY_TO, to);
args.put(KEY_LINK, link);
args.put(KEY_DIST, dist);
return db.update(DATABASE_TABLE, args,
KEY_ROWID + "=" + rowId, null) > 0;
}
}
答案 0 :(得分:0)
您尚未提供stacktrace或logcat错误。 但是,您的代码也会显示一些直接错误。
public static final String KEY_FROM = "fromtime";
public static final String KEY_TO = "totime";
只需查看这些最终的String值以及您在DATABASE_CREATE中声明的内容:
from text not null, to not null "
当您在使用insert / update或其他任何内容时使用相同的String值时,属性名称(from,to)不匹配。
希望它有所帮助。