Describing the creation of a table in the query string, I forgot to put a space between EXISTS and the name of the table.
Corrected the error - put a space. However, android studio continues to produce the same error, indicating the old line, where there is no space.
I tried to fix it in other ways: I deleted the application from the emulator, made sync gradle, rebuild procject, clean project, invalidate cache / restart, even restarted the computer. The error remains the same.
java.lang.RuntimeException: Unable to start activity ComponentInfo{e.mi.work3/e.mi.work3.MainActivity}: android.database.sqlite.SQLiteException: near "EXISTSstudent": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTSstudent (id INTEGER PRIMARY KEY, fio TEXT, time TEXT)
A class that describes the table and contains the same row for the SQL_CREATE_ENTRIES
query SQL_CREATE_ENTRIES
package e.mi.work3.Data; import android.provider.BaseColumns; public class Student { private int ID; private String FIO; private String time; public static class StudentEntry implements BaseColumns { public final static String TABLE_NAME = "student"; public final static String COLUMN_ID = "id"; public final static String COLUMN_FIO = "fio"; public final static String COLUMN_TIME = "time"; public static final String SQL_CREATE_ENTRIES = "CREATE TABLE IF NOT EXISTS " + StudentEntry.TABLE_NAME + " (" + StudentEntry.COLUMN_ID + " INTEGER PRIMARY KEY, " + StudentEntry.COLUMN_FIO + " TEXT, " + StudentEntry.COLUMN_TIME + " TEXT)"; private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + StudentEntry.TABLE_NAME; public static String getSqlCreateEntries() { return SQL_CREATE_ENTRIES; } public static String getSqlDeleteEntries() { return SQL_DELETE_ENTRIES; } } public int getID() { return ID; } public void setID(int ID) { this.ID = ID; } public String getFIO() { return FIO; } public void setFIO(String FIO) { this.FIO = FIO; } public String getTime() { return time; } public void setTime(String time) { this.time = time; }
}
The class that creates the table.
package e.mi.work3.database; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.List; import e.mi.work3.Data.Student; public class StudentsDbHelper extends SQLiteOpenHelper { public static final int DATABASE_VERSION = 1; public static final String DATABASE_NAME = "Students.db"; public StudentsDbHelper(Context context) { super(context,DATABASE_NAME,null,DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(Student.StudentEntry.getSqlCreateEntries()); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL(Student.StudentEntry.getSqlDeleteEntries()); onCreate(db); } @Override public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { onUpgrade(db, oldVersion, newVersion); } public long insertStudent(String ID, String FIO, String time) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(Student.StudentEntry.COLUMN_ID,ID); values.put(Student.StudentEntry.COLUMN_FIO,FIO); values.put(Student.StudentEntry.COLUMN_TIME,time); long rawId = db.insert(Student.StudentEntry.TABLE_NAME, null, values); return rawId; } public ArrayList<Student> getAllStudent() { ArrayList<Student> studentList = new ArrayList<>(); SQLiteDatabase db = this.getReadableDatabase(); String query = "SELECT * FROM " + Student.StudentEntry.TABLE_NAME + " ORDER BY " + Student.StudentEntry.COLUMN_TIME; Cursor cursor = db.rawQuery(query,null); if(cursor.moveToFirst()) { do { Student student = new Student(); student.setID(cursor.getInt(cursor.getColumnIndex(Student.StudentEntry.COLUMN_ID))); student.setFIO(cursor.getString(cursor.getColumnIndex(Student.StudentEntry.COLUMN_FIO))); student.setTime(cursor.getString(cursor.getColumnIndex(Student.StudentEntry.COLUMN_TIME))); studentList.add(student); } while (cursor.moveToNext()); } db.close(); return studentList; } public void deleteAllStudents(){ int rowsNumberDeleted; SQLiteDatabase db = this.getWritableDatabase(); rowsNumberDeleted = db.delete(Student.StudentEntry.TABLE_NAME,null,null); Log.i("info",String.valueOf(rowsNumberDeleted) + " students is deleted from database while launching"); }
}
UPD
I sat all day on this error. Just now I noticed a strange connection: in MainACtivity, I get an array of objects that I fill with data through getAllStudents in the StudentsDbHelper class. In MainActivity I can display the data of this object in the log. So everything is in order here.
However, I need to get an array of these objects in another activity. To do this, I have a static getStudentListInstance () method in mainActivity. The second activity is recyclerView. When the adapter in the second activation accepts the list of objects previously created in MainActivity via MainActivity.getStudentListInstance (), everything goes well. However, if you uncomment the line in which I INSTALL adapter for recyclerView, paradoxically, the error described above occurs.
class MainActivity
package e.mi.work3; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Parcelable; import android.provider.BaseColumns; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import java.util.ArrayList; import java.util.List; import e.mi.work3.Data.Student; import e.mi.work3.Data.Util; import e.mi.work3.database.StudentsDbHelper; public class MainActivity extends AppCompatActivity { public static ArrayList<Student> studentList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); StudentsDbHelper dbHelper = new StudentsDbHelper(this); dbHelper.deleteAllStudents(); for(int i = 0; i < 5; i++) { dbHelper.insertStudent(String.valueOf(i + 1), Util.getStudentRandomRaw(), Util.getCurrentTime()); } studentList = dbHelper.getAllStudent(); Log.i("info",studentList.get(0).getFIO()); Intent intent = new Intent(this, DatabaseInfoActivity.class); startActivity(intent); } public static ArrayList<Student> getStudentListInstance() { return studentList; }
}
Second Activation Class
package e.mi.work3; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import java.util.ArrayList; import e.mi.work3.Data.Student; import e.mi.work3.database.StudentsDbHelper; public class DatabaseInfoActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.recyclerview_student); MyAdapter adapter = new MyAdapter(MainActivity.getStudentListInstance(),this); RecyclerView rv = findViewById(R.id.recyclerViewStudent); rv.setLayoutManager(new LinearLayoutManager(this)); // rv.setAdapter(adapter); //**Здесь и возникает ошибка.** }
}
Adapter code (in response to a comment in question) `package e.mi.work3;
import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import org.w3c.dom.Text; import java.util.ArrayList; import e.mi.work3.Data.Student; public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> { ArrayList<Student> data; Context m; @Override public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_student ,viewGroup,false); return new MyViewHolder(v); } @Override public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) { myViewHolder.id.setText(data.get(i).getID()); myViewHolder.fio.setText(data.get(i).getFIO()); myViewHolder.time.setText(data.get(i).getTime()); } public class MyViewHolder extends RecyclerView.ViewHolder { TextView id; TextView fio; TextView time; public MyViewHolder(View v) { super(v); id = v.findViewById(R.id.idStudent); fio = v.findViewById(R.id.fioStudent); time = v.findViewById(R.id.timeStudentAdded); } } public MyAdapter(ArrayList<Student> data, Context m) { this.data = data; this.m = m; } @Override public int getItemCount() { return data.size(); }
} `