Hello! I'm new to programming, I hope for your advice. A simple application for notes. By clicking on fab there should be an entry in the database of 2 lines: the title (title) and the note itself (note). But the application crashes when you click on fab.

The method that makes the data in the database

fabOnCreatePage.setOnClickListener(new View.OnClickListener() { String title = editTitle.getText().toString(); String note = editNote.getText().toString(); SQLiteDatabase database = dataBase.getWritableDatabase(); ContentValues contentValues = new ContentValues(); @Override public void onClick(View view) { switch (view.getId()) { case R.id.fab_on_create_note: contentValues.put(DataBase.DB_TITLE, title ); contentValues.put(DataBase.DB_NOTE, note); database.insert(DataBase.DB_NAME, null, contentValues); break; } } } 

And itself bd (without onUpgrade)

  public class DataBase extends SQLiteOpenHelper { public static final String DB_NAME = "db"; public static final int DB_VERSION = 1; public static final String DB_TITLE = "title"; public static final String DB_NOTE = "note"; public DataBase(Context context) { super(context, DB_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL("create table notes(" + "_id integet primary key autoincrement, " + DB_TITLE + "text," + DB_NOTE + "text)"); } 

Error Message (I translated it, but still not clear)

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.idrisov.notes/com.example.idrisov.notes.CreateNote}: android.database.sqlite.SQLiteException: (code 1): , while compiling: create table notes(_id integet primary key autoincrement, titletext,notetext) 
  • I think the fact is that you have no spaces in front of "text , which leads to the non - valid expression of creating a database - YuriySPb
  • one
    intege t -> intege r - Yura Ivanov
  • @YuraIvanov thank you. Himself ashamed of such a ridiculous mistake. Now it seems to work - Tagir Idrisov
  • one
    By the way about the spaces, they do not affect the validity, but affect the meaning. The columns in sqlite are all string, the specified types are affinity. This query will simply create a table with _id , titletext and notetext In this case, the last two columns will be without affinity and you can write anything, just like the others ... And depending on what is written, it will be interpreted and interpreted accordingly. Illustration sqlfiddle.com/#!7/8c8ae/1 Column d is just the same and can contain both text and integer . - Yura Ivanov
  • one
    @YuraIvanov, it seems to me that if you make your comments in response, it’s good and useful to succeed) - YuriySPb

1 answer 1

First, you have the syntax error intege t -> intege r .
Secondly, the actual error. There are not enough spaces between title and text , and also between note and text .

 create table notes(_id integer primary key autoincrement, titletext,notetext) 

This query will create a table with three fields _id , titletext and notetext . There will be no syntax error.

In sqlite, all columns are string, specifying types for columns is only affinity , i.e. rules for interpreting values ​​in queries. And they may not be listed at all. Moreover, if these types are still specified when creating the table, you can still record data of any type.

SqlFiddle - here column d without specifying affinity behaves both as string and as numeric, depending on the data written to it.

In addition, it should be noted that without affinity (or when storing in the integer column both numbers and strings at the same time), there may be various surprises when sorting. For example, 100<'100' and 500<'1' , because number less than any string.

Thus, a query without spaces creates a table, but the expected columns title and note will not be in it, so be careful with concatenations ...