Office Hours Transcript: 2021-11-11

john joined

hello, john!

 

how can I help you today?

Hello Mark,

I’m having issues with important a database to room. Here is the error message:

 
 Caused by: java.lang.IllegalStateException: Pre-packaged database has an invalid schema: excerpt(com.talisol.sapporosunsetwakeup2.data.Excerpt).
 Expected:
TableInfo{name='excerpt', columns={english=Column{name='english', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, number=Column{name='number', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, original=Column{name='original', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, source=Column{name='source', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
 Found:
TableInfo{name='excerpt', columns={number=Column{name='number', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, original=Column{name='original', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, english=Column{name='english', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1, defaultValue='null'}, source=Column{name='source', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
 

It seems like there is an issue with the notNull parameter

 

I tried both setting @NotNullable in the data class and when I make the DB in DB browser, but it doesn’t seem to make a difference.

I usually create my empty database using Room itself, whether via an instrumented test or something in the app

 

that way, I am 100% guaranteed that the schema matches

 

I might then populate the database using DB Browser or other tools

 

what would be cool is if somebody wrote a Gradle plugin or something that took the exported schema JSON and created an empty database for you

 

but, in this case, your problem seems to be in DB Browser, where your NOT NULL requests are not taking effect

That’s smart, let me try that.

 

I have an issue because I’m trying to prepopulate the DB with the content of a csv file. Since the primary is an id that needs to be autoincremented, the only way to do import the csv is to set the id collumn to notNullable = false in DB browser. Is it possible to do the same in room?

IIRC, Room needs the primary key to be NOT NULL

 

however, I do not see why importing data requires a nullable ID

 

at the end of the day, importing data is doing INSERT statements

 

and an auto-incremented primary key works fine with INSERT statements

 

it may be that the specific way you are trying to import the CSV has problems, but that is an issue with the approach, not the database

is there a way to do the insert programmatically directly in ROOM? (i.e., without using a 3rd party tool like DB Browser)

one option would be to pre-package the CSV, rather than the SQLite database, and write code to import the data yourself – I do that in the first FTS sample in Elements of Android Room

 

another option is to write some code (Gradle task, CLI app, etc.) that does the import for you outside of DB Browser, where you control the INSERT statements

 

that code could either use a SQLite API, or it could generate a .sql file containing the INSERT statements that you then run using sqlite3

I’m going to try the 1st option. In my browser, Ctrl F doesn’t return anything for "CSV" or "pre-package", would you mind letting me know what section/page I should look at?

in my case, the data is not starting as CSV, but it is still a manual import of the data

 

it is part of the material in the "Room and Full-Text Search" chapter

 

and the FTS sample module

Jan joined

Hi Mark and John

hello, Jan! how can I help you today?

I’m doing wifi connection. It works on 10 but on 12 I always get "unavailable" in the callback. Same code. Documentation says "unavailable" might be capabilities don’t match. Does 12 require an exact match of capabilities in the request object?

I have no idea, sorry

Have you ever done this:
val specifier = WifiNetworkSpecifier.Builder()
.setSsid(ssid)
.setWpa2Passphrase(key)
.build()
val request = NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) // must be local network
.setNetworkSpecifier(specifier)
.build()
val callback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {

 
        connectivityManager.requestNetwork(
            request,
            callback,
            35000
        )  // needs some time to discover

john left

 

john joined

I have used registerNetworkCallback() and registerDefaultNetworkCallback(). They seem to work normally on Android 12, at least for my use case.

grrigore joined

I played around with WifiNetworkSpecifier, but we backed that code out after it wasn’t working with a test Samsung device, so I have not used it on Android 12 (and it was only for matching networks based on a prefix, not with a full SSID and passphrase).

hello!

ggrigore: hi! we’re running out of chat time, so I’ll try to handle questions as they arrive – do you have a question?

quick question, Mark. did you get the chance to try sending a SMS programmatically on newer android versions?

I have used both the forms of register also. Register won’t work if I pass in the specifier but will work without it. Neither will display the ssid - says "unknown ssid". Do you get that on 12 too?

ggrigor: I haven’t played with SmsManager in years, sorry

 

Jan: I get "unknown SSID" without location permissions, but that is not new to Android 12

no problem, thank you!

Okay. 12 is showing location permissions enabled. THanks.

 

I meant the app shows it is enabled.

Thank you for your time Mark.

john left

 

Jan left

have a nice day, Mark! or night I’m not sure :D

 

and have a nice day/night/whatever, too!

oh yes, it’s morning right?

 

around 9 AM?

9:30, but yes

i m 7 hours ahead

well, then, have a pleasant late afternoon!

 

(or early evening, if you prefer)

haha thank you! goodbye

grrigore left