开发者

android sqlite adding an item

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 T开发者_Python百科ABLE 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;
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜