在光标所在行强制关闭!

发布于 2024-10-25 22:40:10 字数 6531 浏览 0 评论 0原文

嘿! 我正在尝试创建一个应用程序来查找存储在 SQlite 数据库中的 GPS 数据。 但我面临一个问题: 我构建了一个 DbAdapter 类来创建我的数据库,现在我正在尝试从另一个类获取所有数据上的光标,使用此函数:

public Cursor fetchAllData() {

        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_LONGITUDE,KEY_LATITUDE,KEY_COUNTRY,KEY_TOWN,KEY_STREET}, null, null, null, null, null);
    }

现在,我正在我的新类中创建 DbAdapter 的实例,但我得到了强制关闭当我插入这一行时:Cursor c=db.fetchAllData();

创建数据库的类如下所示:

package test.android;

import java.util.ArrayList;
import java.util.List;

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 CoordonateDbAdapter {


    public static final String KEY_ROWID = "_id";
    public static final String KEY_LONGITUDE= "longitude";
    public static final String KEY_LATITUDE = "latitude";
    public static final String KEY_COUNTRY= "country";
    public static final String KEY_TOWN= "town";
    public static final String KEY_STREET = "street";

    private static final String TAG = "CoordonateDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    private static final String DATABASE_CREATE =
            "create table car1 (_id integer primary key autoincrement, "
                    + "longitude text not null, latitude text not null," +
                   "country text not null,town text not null,street text not null);";

    private static final String DATABASE_NAME = "gps";
    private static final String DATABASE_TABLE = "masini";
    private static final int DATABASE_VERSION = 1;

    private final Context mCtx;

    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 notes");
            onCreate(db);
        }
    }
    public CoordonateDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }
    public CoordonateDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }
    public long insertData(String longitude, String latitude, String country, String town, String street) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_LONGITUDE, longitude);
        initialValues.put(KEY_LATITUDE, latitude);
        initialValues.put(KEY_COUNTRY, country);
        initialValues.put(KEY_TOWN, town);
        initialValues.put(KEY_STREET, street);

        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }
    public boolean deleteData(long rowId) {

        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }
   public Cursor fetchAllData() {

        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_LONGITUDE,KEY_LATITUDE,KEY_COUNTRY,KEY_TOWN,KEY_STREET}, null, null, null, null, null);
    }
    public Cursor fetchData(long rowId) throws SQLException {

        Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] 
                    {KEY_ROWID, KEY_LONGITUDE,KEY_LATITUDE,KEY_COUNTRY,KEY_TOWN,KEY_STREET}, 
                     KEY_ROWID + "=" + rowId, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
    public boolean updateNote(long rowId, String longitude, String latitude,String country, String town,String street) {
        ContentValues args = new ContentValues();
        args.put(KEY_LONGITUDE, longitude);
        args.put(KEY_LATITUDE, latitude);
        args.put(KEY_COUNTRY, country);
        args.put(KEY_TOWN, town);
        args.put(KEY_STREET, street);

        return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }

    public List<String> selectAll() {

       List<String> list = new ArrayList<String>();
       Cursor cursor = this.mDb.query(DATABASE_TABLE, new String[] { "longitude"},

         null, null, null, null, "name desc");

       if (cursor.moveToFirst()) {

          do {

             list.add(cursor.getString(0));

          } while (cursor.moveToNext());

       }
       if (cursor != null && !cursor.isClosed()) {

          cursor.close();

       }

       return list;

    }
}

检索 gps 数据的类如下所示:

package test.android;

import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Toast;
public class screen_database extends Activity{

      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.screen_database);
           CoordonateDbAdapter db = new CoordonateDbAdapter(this); 
          db.open();
          long id;
      //    id= db.insertData("-36.2", "125","Romania","Cluj","Zorilor");
      //    db.insertData("44", "55","Romania","Iasi","Alexandru Ioan Cuza");
       //   List<String> names = db.selectAll();
           Cursor c=db.fetchAllData();

         /*  if (c.moveToFirst())
            {
                do {          
                  //  DisplayTitle(c);
                } while (c.moveToNext());
            }*/
         //   c.close();

        }

      /*  public void DisplayTitle(Cursor c)
        {
            Toast.makeText(this, 
                    "longitude: " + c.getString(0) + "\n" +
                    "latitude: " + c.getString(1) + "\n" +
                    "country: " + c.getString(2) + "\n" +
                    "town:  " + c.getString(3),
                    Toast.LENGTH_LONG).show();        
        }*/ 
}
//}

您可以看到很多注释行,因为当我请求游标时我会强制关闭,所以我试图保留就这么简单。 我是否需要任何权限才能使用光标,因为我在互联网上查看并且所有行代码看起来都像我的?! 另一个问题是我的应用程序相当大,并且我正在从其他类访问这些类......一长排类,直到我从 Sqlite 进行查询。 如果您能帮助我,我将非常感激,这可能很简单,但我不知道它是什么。谢谢!

Hy!
I'm trying to create an application that looks for gps data that was stored in SQlite database.
But I'm facing a problem:
I built an DbAdapter class that creates my database and now I'm trying from another class to get an cursor over my all data,using this function:

public Cursor fetchAllData() {

        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_LONGITUDE,KEY_LATITUDE,KEY_COUNTRY,KEY_TOWN,KEY_STREET}, null, null, null, null, null);
    }

Now,I'm creating an instance of DbAdapter in my new class,but I get forceclose when I insert this line:Cursor c=db.fetchAllData();

The class that creates my database looks like this:

package test.android;

import java.util.ArrayList;
import java.util.List;

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 CoordonateDbAdapter {


    public static final String KEY_ROWID = "_id";
    public static final String KEY_LONGITUDE= "longitude";
    public static final String KEY_LATITUDE = "latitude";
    public static final String KEY_COUNTRY= "country";
    public static final String KEY_TOWN= "town";
    public static final String KEY_STREET = "street";

    private static final String TAG = "CoordonateDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    private static final String DATABASE_CREATE =
            "create table car1 (_id integer primary key autoincrement, "
                    + "longitude text not null, latitude text not null," +
                   "country text not null,town text not null,street text not null);";

    private static final String DATABASE_NAME = "gps";
    private static final String DATABASE_TABLE = "masini";
    private static final int DATABASE_VERSION = 1;

    private final Context mCtx;

    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 notes");
            onCreate(db);
        }
    }
    public CoordonateDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }
    public CoordonateDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }
    public long insertData(String longitude, String latitude, String country, String town, String street) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_LONGITUDE, longitude);
        initialValues.put(KEY_LATITUDE, latitude);
        initialValues.put(KEY_COUNTRY, country);
        initialValues.put(KEY_TOWN, town);
        initialValues.put(KEY_STREET, street);

        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }
    public boolean deleteData(long rowId) {

        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }
   public Cursor fetchAllData() {

        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_LONGITUDE,KEY_LATITUDE,KEY_COUNTRY,KEY_TOWN,KEY_STREET}, null, null, null, null, null);
    }
    public Cursor fetchData(long rowId) throws SQLException {

        Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] 
                    {KEY_ROWID, KEY_LONGITUDE,KEY_LATITUDE,KEY_COUNTRY,KEY_TOWN,KEY_STREET}, 
                     KEY_ROWID + "=" + rowId, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
    public boolean updateNote(long rowId, String longitude, String latitude,String country, String town,String street) {
        ContentValues args = new ContentValues();
        args.put(KEY_LONGITUDE, longitude);
        args.put(KEY_LATITUDE, latitude);
        args.put(KEY_COUNTRY, country);
        args.put(KEY_TOWN, town);
        args.put(KEY_STREET, street);

        return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }

    public List<String> selectAll() {

       List<String> list = new ArrayList<String>();
       Cursor cursor = this.mDb.query(DATABASE_TABLE, new String[] { "longitude"},

         null, null, null, null, "name desc");

       if (cursor.moveToFirst()) {

          do {

             list.add(cursor.getString(0));

          } while (cursor.moveToNext());

       }
       if (cursor != null && !cursor.isClosed()) {

          cursor.close();

       }

       return list;

    }
}

And the class that retrieves the gps data is like this:

package test.android;

import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Toast;
public class screen_database extends Activity{

      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.screen_database);
           CoordonateDbAdapter db = new CoordonateDbAdapter(this); 
          db.open();
          long id;
      //    id= db.insertData("-36.2", "125","Romania","Cluj","Zorilor");
      //    db.insertData("44", "55","Romania","Iasi","Alexandru Ioan Cuza");
       //   List<String> names = db.selectAll();
           Cursor c=db.fetchAllData();

         /*  if (c.moveToFirst())
            {
                do {          
                  //  DisplayTitle(c);
                } while (c.moveToNext());
            }*/
         //   c.close();

        }

      /*  public void DisplayTitle(Cursor c)
        {
            Toast.makeText(this, 
                    "longitude: " + c.getString(0) + "\n" +
                    "latitude: " + c.getString(1) + "\n" +
                    "country: " + c.getString(2) + "\n" +
                    "town:  " + c.getString(3),
                    Toast.LENGTH_LONG).show();        
        }*/ 
}
//}

You can see lots of lines that are comments because I get force close when I'm asking for a Cursor,so I tried to keep it as simple.
Do I need any permissions to work with cursors,becuase I looked on the internet and all the line code looks like mine?!
Another problem is that my application is quite big,and I'm accesing this classes from other classes....a long row of classes until I get to query from the Sqlite.
I would really apreciate if you would help me,it might be something simple but I can't figure it out what it is.Thank you!!!

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

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

发布评论

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

评论(1

静待花开 2024-11-01 22:40:10

首先尝试通过复制直接访问,

    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_LONGITUDE,KEY_LATITUDE,KEY_COUNTRY,KEY_TOWN,KEY_STREET}, null, null, null, null, null);

而不是通过方法调用。
如果有效,请检查该方法中的curser 是否可用。
如果是,请尝试减少 SQLQuery 之类的内容

    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_LONGITUDE}, null, null, null, null, null);

并逐步添加更多列。

first try to access directly by copying

    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_LONGITUDE,KEY_LATITUDE,KEY_COUNTRY,KEY_TOWN,KEY_STREET}, null, null, null, null, null);

instead of calling it by method.
If that works, check if curser is available in the method.
If it is, try to reduce the SQLQuery like

    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_LONGITUDE}, null, null, null, null, null);

and add more columns step by step.

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