sugar: No such table
Hello, i have big problem with Sugar ORM implementation in my project. After hours of searching for working solution, i created new clean project to be sure, that none of another modules don’t break sugar implementation. On new project with only implemented sugar i have still the same error “no such table”. I tried lot of times changing VERSION metadata, extending app class with SugarApp, deleting whole app data, reinstalling app. Instant run is disabled. Problem occurs on Android Studio 1.5 with Gradle Plugin <2 and Android Studio 2.1.1 with Gradle plugin 2.1.0. Here is code of this test project, that generates this error too:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button test = (Button) findViewById(R.id.test);
test.setOnClickListener(listener);
}
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
SugarContext.init(getApplicationContext());
List<Test> testList = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
testList.add(new Test("Name "+i, "Content "+i));
}
Test.saveInTx(testList);
testList = Test.listAll(Test.class);
Test.deleteInTx(testList);
} catch (Exception e) {
e.printStackTrace();
new AlertDialog.Builder(MainActivity.this).setMessage(e.getMessage()).create().show();
}
}
};
class Test extends SugarRecord {
public String name;
public String content;
public Test() {
}
public Test(String name, String content) {
this.name = name;
this.content = content;
}
}
}
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<meta-data android:name="DATABASE" android:value="sugarorm.db" />
<meta-data android:name="VERSION" android:value="2" />
<meta-data android:name="QUERY_LOG" android:value="true" />
</application>
For request i can send zipped project. Thanks in advance
About this issue
- Original URL
- State: open
- Created 8 years ago
- Comments: 26 (2 by maintainers)
Im having the same problem, but only happens with os 5.0 (API level 21) and above. Using Android Studio 2.1.1 and try to downgrade my graddle from 2.1 to 2.0 but still the same problem.
So I was very surprise about this cause this is working in another project no problems at all. The only difference was then the target sdk (maximun)
Momentaneously fix it by lowering the targetSdkVersion to 21 instead of 23
So probably the problem has something to do with the instant run.
I wish with all my heart you can fix this, I have compare with others ORM and sugar is my choice above all, Im very sorry for not being good enough to do a pull request, please fix it.
If nothing else works, you could create the tables manually.
SugarRecord.executeQuery("CREATE TABLE IF NOT EXISTS YOUR_CLASS_NAME (ID INTEGER PRIMARY KEY AUTOINCREMENT, YOUR_COLUMN_NAME TEXT, ANOTHER_COLUMN_NAME INTEGER)");@larrytech7 I finally found solution with your big help. In project MUST be declared class extended by SugarApp (in fact it’s application class) like this (overridden functions not required, but I want have some code inside class):
This application class must be specified in AndroidManifest.xml as master Application class like this:
And it works awesome! I don’t know why i thought, that SugarContext.init(application) will be work anywhere and allows me to initialize where I want too.
@cutiko with this implementation I successfully run with targetSdkVersion 23. I use 1.5 version library.
Thank you everyone for your help and dedicated time.
The hotfix for this trouble is around the feature of Android Studio InstantRun. If you disable it, and run the avd you will see your app running with no trouble.
This bug has been solved with a PR, but it’s not included in the latest version of sugarOrm.
I have the same issue, the weird thing is my error in logcat was shown like below while there is no A, B, C, D tables in my code (exactly my table class name is “BlockTable”):
“no such table: A Error in saving in transaction no such table: A (code 1): , while compiling: INSERT OR REPLACE INTO A(D,A,B,C) VALUES (?,?,?,?)”
Another weird thing that everything working good while running debug, but when i run in release mode then the app crash by the error above.