android sqlite添加一个项目

发布于 2024-10-23 18:27:23 字数 6669 浏览 1 评论 0原文

大家好,我有一个小问题,我是 android 新手,我正在尝试实现我在 这个 站点...

我的问题是我在示例中添加了 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;
    }
}

hi to all i have a small problem i am new to android and i was trying to implement and sqlite example i found on this site...

my problem is that i add a 2 columns extra to the example i found the app crashes can any one please tell me what is my mistake

this is the code:

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();  
    } 

}

and this is the 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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

用心笑 2024-10-30 18:27:23

您尚未提供堆栈跟踪或 logcat 错误。
然而,您的代码也显示了一些直接的错误。

  1. public static final String KEY_FROM = "fromtime";
    public static Final String KEY_TO = "totime";

只需查看这些最终 String 值以及您在 DATABASE_CREATE 中声明的内容:

from text not null, to not null "

当您要使用相同的 String 值时,属性名称(from、to)不匹配在使用插入/更新或其他方式时。

  1. DATABASE_CREATE 字符串中缺少逗号。

希望有帮助。

You haven't provided your stacktrace or logcat error.
However your code also shows some straight forward errors.

  1. public static final String KEY_FROM = "fromtime";
    public static final String KEY_TO = "totime";

just look at these final String values and what you have declared inside DATABASE_CREATE :

from text not null, to not null "

The attribute name (from,to) does not match when you are going to use the same String values while using insert/update or whatever.

  1. You are missing a comma inside the DATABASE_CREATE string.

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文