标签: android-sqlite

麻烦与大量的代码重复

我有很多方法在大部分内容遵循相同的算法,理想情况下我希望能够调用一个消除大量代码重复的泛型方法.

我有很多类似下面的方法,我最好希望能够只调用 Save<SQLiteLocation>(itemToSave);但是我有很多麻烦,因为这些方法 SQLiteConnection.Find<T>不会接受像泛型中的T这样的抽象数据类型.

有没有办法解决这个问题,如果我能解决这个问题,我会节省多达150行代码

这是我的代码:

    public bool SaveLocation(ILocation location, ref int primaryKey)
    {
        var dbConn = new SQLiteConnection (dbPath);

        SQLiteLocation itemToSave = new SQLiteLocation ();
        itemToSave.LocationName = location.LocationName;
        itemToSave.Latitude = location.Latitude;
        itemToSave.Longitude = location.Longitude;
        itemToSave.PrimaryKey = location.PrimaryKey;

  ----------------------------------------------------------------------------------------

        SQLiteLocation storedLocation = dbConn.Find<SQLiteLocation>
                                        (x => x.PrimaryKey == location.PrimaryKey);

        if (storedLocation != null)
        {
            dbConn.Update(itemToSave);
            return true;
        }

        else if (storedLocation == null)
        {
            dbConn.Insert(itemToSave);
            primaryKey = itemToSave.PrimaryKey;
            return true;
        }
        return false;
    }
Run Code Online (Sandbox Code Playgroud)

在这里另一种方法看看我的虚线下面的两种方法中的代码基本上是一样的

    public bool SaveInvitation(IInvitation invitation, …
Run Code Online (Sandbox Code Playgroud)

c# generics maintainability android-sqlite

6
推荐指数
1
解决办法
138
查看次数

Android - SQLiteDatabase配置数据库错误警告

我正面临一个与SQLiteDatabase申请有关的问题.应用程序没有崩溃.这是我在Logcat中收到的日志.

 D/SQLiteDatabase:dispose database false
            java.lang.Throwable: stacktrace
            at android.database.sqlite.SQLiteDatabase.dispose(SQLiteDatabase.java:286)
            at android.database.sqlite.SQLiteDatabase.onAllReferencesReleased(SQLiteDatabase.java:280)
            at android.database.sqlite.SQLiteClosable.releaseReference(SQLiteClosable.java:74)
            at android.database.sqlite.SQLiteClosable.close(SQLiteClosable.java:106)
            at com.demo.model.SQLiteManager.close(SQLiteManager.java:74)
            at com.demo.ui.ContactFragment$ContactListAdapter.fetchContactData(ContactFragment.java:295)
            at com.demo.ui.ContactFragment$ContactListAdapter.<init>(ContactFragment.java:336)
            at com.demo.ui.ContactFragment$4.onQueryTextChange(ContactFragment.java:224)
            at android.support.v7.widget.SearchView.onTextChanged(SearchView.java:1150)
            at android.support.v7.widget.SearchView.access$2000(SearchView.java:103)
            at android.support.v7.widget.SearchView$12.onTextChanged(SearchView.java:1680)
            at android.widget.TextView.sendOnTextChanged(TextView.java:8340)
            at android.widget.TextView.setText(TextView.java:4413)
            at android.widget.TextView.setText(TextView.java:4252)
            at android.widget.EditText.setText(EditText.java:90)
            at android.widget.TextView.setText(TextView.java:4227)
            at android.support.v7.widget.SearchView.setQuery(SearchView.java:585)
            at com.demo.ui.ContactFragment.onPause(ContactFragment.java:153)
            at android.support.v4.app.Fragment.performPause(Fragment.java:2139)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1117)
            at android.support.v4.app.FragmentManagerImpl.detachFragment(FragmentManager.java:1408)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:727)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
            at android.support.v4.view.ViewPager$3.run(ViewPager.java:254)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:912)
            at android.view.Choreographer.doCallbacks(Choreographer.java:714)
            at android.view.Choreographer.doFrame(Choreographer.java:646)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:898)
            at android.os.Handler.handleCallback(Handler.java:815)
            at android.os.Handler.dispatchMessage(Handler.java:104)
            at android.os.Looper.loop(Looper.java:207)
            at android.app.ActivityThread.main(ActivityThread.java:5769) …
Run Code Online (Sandbox Code Playgroud)

android android-sqlite

6
推荐指数
0
解决办法
1068
查看次数

Android错误getSlotFromBufferLocked:未知缓冲区:0xa9fb8d90

我正在开发一个Android应用程序,它使用介绍幻灯片的材质介绍依赖项,但当幻灯片完成并尝试从幻灯片(MaterialintroActivity)切换到我的主要活动时,应用程序崩溃.

logcat的

 07-26 12:42:19.566 897-944/com.naive.LISTY E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa9fb8d90
Run Code Online (Sandbox Code Playgroud)

AppManifest代码

<application
    android:allowBackup="true"
    android:icon="@mipmap/icon"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainIntroActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActivity"/>
</application>
Run Code Online (Sandbox Code Playgroud)

MainIntroActivity

public class MainIntroActivity extends IntroActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {

    setFullscreen(true);
    super.onCreate(savedInstanceState);

    addSlide(new SimpleSlide.Builder()
            .title(R.string.title)
            .description(R.string.description)
            .image(R.mipmap.ic_launcher)
            .background(R.color.background)
            .backgroundDark(R.color.background_dark)
            .buttonCtaClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    Intent main=new Intent(MainIntroActivity.this, MainActivity.class);
                    startActivity(main);
                }
            })
            .build());
}}
Run Code Online (Sandbox Code Playgroud)

MainActivity代码

public class MainActivity extends AppCompatActivity { …
Run Code Online (Sandbox Code Playgroud)

android android-sqlite

6
推荐指数
1
解决办法
2万
查看次数

为什么ContentProvider在App更新后返回空游标?

我最近为我的应用程序发布了一个更新,并且我收到一些用户的反馈,称他们的数据丢失了。经过分析,我确定我使用的单个内容提供程序始终返回一个空游标。

我没有更改内容提供者本身的代码或查询它的代码。数据格式没有改变。该问题仅在少数设备上可见,并且似乎仅限于Android 6.0(主要是三星,但也包括某些LG设备)。不仅会丢失初始数据,而且所有后续插入操作也不会持久化。

可以通过全新安装应用(删除+安装)解决此问题。

有谁知道如何解决/解决方法?

我认为这无关紧要,但这是代码:

Cursor cursor = contentResolver.query(Columns.contentUri(), Columns.ALARM_QUERY_COLUMNS, null, null, Columns.DEFAULT_SORT_ORDER);

List<AlarmActiveRecord> alarms = new ArrayList<AlarmActiveRecord>();
try {
    if (cursor.moveToFirst()) {
        do {
            alarms.add(factory.create(cursor));
        } while (cursor.moveToNext());
    }
} finally {
    cursor.close();
}
return alarms;
Run Code Online (Sandbox Code Playgroud)

和内容提供者:

@Override
public Cursor query(Uri url, String[] projectionIn, String selection, String[] selectionArgs, String sort) {
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    // Generate the body of the query
    Preconditions.checkArgument(sURLMatcher.match(url) == ALARMS, "Invalid URL %s", url);
    qb.setTables("alarms");

    SQLiteDatabase db = …
Run Code Online (Sandbox Code Playgroud)

java android android-contentprovider android-sqlite

6
推荐指数
1
解决办法
183
查看次数

无法在事务内附加数据库

我正在开发一个必须使用两个数据库的应用程序。我想将新数据库表中的记录添加到旧数据库表中,我已将新数据库放在项目的资产文件夹中,并且可以使用新数据库和旧数据库进行查询。

问题是当我尝试在方法上将新数据库附加到旧数据库时,发生了无法在事务中附加数据库的onUpgrade()错误。

我发现很多链接都有这个问题,我已经尝试了很多但没有成功。我是 Android 新手SQLite,请告诉我如何在代码中附加数据库和另一个数据库。

数据库助手类: DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {
Context context;
SQLiteDatabase sourceDatabase;
private String TAG = this.getClass().getSimpleName();

private static final String DATABASE_NAME = "namesdemo_db";
private static final String SOURCE_DB_NAME = "namesdemo_db_s";
private static final int DATABASE_VERSION = 23;

private String DATABASE_PATH;
File dbFile;

// TABLE NAMES
private static final String TABLE_EMPLOYEE = "employee";
private static final String TABLE_FAVORITE = "fevorite";

// KEYS of Table Employee
private static final String KEY_ID …
Run Code Online (Sandbox Code Playgroud)

sqlite android android-sqlite

5
推荐指数
1
解决办法
5579
查看次数

如何重新加载/刷新已经显示过一次的片段?

在此输入图像描述

  • 我有2 Fragment秒:My1stFragment和My2ndFragment.
  • My1stFragment我有一个ListView由数据库填充的内部.此外,当我点击编辑按钮时,我调用第二个Fragment来编辑数据.
  • 在第二个Fragment我编辑我的工资金额并通过点击提交按钮更新我的数据库并返回My1stFragment.
  • 问题是,当我回来时,即使数据库已更新My1stFragment,我ListView也没有更新.

android android-listview android-fragments android-viewpager android-sqlite

5
推荐指数
2
解决办法
4753
查看次数

如何使用除主键之外的源属性在GreenDAO中进行多对多关系查询?

假设我们有以下实体:项目:

class Item {
...
    @Index(unique=true)
    private String guid;
...
    @ToMany
    @JoinEntity(entity = JoinItemsWithTags.class, sourceProperty = "itemGuid", targetProperty = "tagName")
    private List<Tag> tagsWithThisItem;
    ...
}
Run Code Online (Sandbox Code Playgroud)

标签:

class Tag {
    @Id
    private Long localId;
    @Index(unique = true)
    private String name;
    ...
}
Run Code Online (Sandbox Code Playgroud)

我们需要加入他们.这是我的连接实体类:

@Entity(nameInDb = "item_tag_relations")
class JoinItemsWithTags {
    @Id
    private Long id;
    private String itemGuid;
    private String tagName;
    ...
}
Run Code Online (Sandbox Code Playgroud)

我想使用标记名称作为连接属性而不是Long id,因为在与服务器同步时更容易支持一致性.但是当前Item类中的标签getter总是返回一个空列表.我查看了日志并找到了在getter中内部使用的生成查询:

SELECT * <<-- there were a long sequence of fields
FROM "tags" T  JOIN item_tag_relations J1 
ON T."_id"=J1."TAG_NAME" …
Run Code Online (Sandbox Code Playgroud)

sqlite android android-sqlite greendao greendao3

5
推荐指数
0
解决办法
186
查看次数

房间库可以从资产文件夹复制数据库吗?

我在资产文件夹中有预加载数据库的旧项目。该项目实现了SQLiteOpenHelper用于数据库操作。但现在要更新应用程序,我想将我的项目移动到Room数据库。

所以我的问题是,Room 库中是否有任何可用的方法或功能,我可以在运行时在应用程序中使用预加载的数据库文件。

或者有什么方法可以先加载数据库,然后从 Room 库直接在该数据库上执行查询?

android android-sqlite android-room

5
推荐指数
2
解决办法
3657
查看次数

如何使用数据库检查器获取查询执行时间?

我正在使用数据库检查器来查询数据库。但是,我想知道优化查询性能的执行时间。有谁知道获取查询的执行时间吗?

android android-sqlite android-studio database-inspector

5
推荐指数
0
解决办法
142
查看次数

将 Inf、NAN 和 -Inf 插入 sqlite3 数据库的 SQL 命令

将无穷大、负无穷大和非数字插入到 sqlite3 数据库表中的实型列中的 sql 命令是什么?

即用什么来代替 value1、value2?

INSERT INTO table (column1,column2 ,..)
VALUES( value1, value2 ,...);
Run Code Online (Sandbox Code Playgroud)

我尝试在 sqlite3文档中查找相关资源,但找不到任何内容。

将“Inf”和“-Inf”作为字符串插入并通过 C++ 读取它会给我一个小的 -0.00... 值

sql sqlite android-sqlite

5
推荐指数
1
解决办法
2510
查看次数