我正在开发一个Android应用程序,当我编程时,没有任何问题或错误,当我在设备上运行它时,logcat:
11-16 09:27:29.930 4261-4261/? I/zygote: Not late-enabling -Xcheck:jni (already on)
11-16 09:27:29.956 4261-4261/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
11-16 09:27:30.188 4261-4261/com.example.root.myapplication I/InstantRun: starting instant run server: is main process
11-16 09:27:30.616 4261-4261/com.example.root.myapplication E/SQLiteLog: (1) near "KEYMatriculeINTEGER": syntax error
11-16 09:27:30.617 4261-4261/com.example.root.myapplication D/AndroidRuntime: Shutting down VM
11-16 09:27:30.619 4261-4261/com.example.root.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.root.myapplication, PID: 4261
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.root.myapplication/com.example.root.myapplication.MainActivity}: android.database.sqlite.SQLiteException: near "KEYMatriculeINTEGER": syntax error (code 1): , while compiling: CREATE TABLE User(id INTEGER PRIMARY KEYMatriculeINTEGER PRIMARY KEYNomTEXTPrenomTEXTTypeTEXT)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.database.sqlite.SQLiteException: near "KEYMatriculeINTEGER": syntax error (code 1): , while compiling: CREATE TABLE User(id INTEGER PRIMARY KEYMatriculeINTEGER PRIMARY KEYNomTEXTPrenomTEXTTypeTEXT)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:890)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:501)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1752)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1682)
at model.sqlite.data.ModelHelper.onCreate(ModelHelper.java:101)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:333)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:262)
at com.example.root.myapplication.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Activity.performCreate(Activity.java:6991)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
主要活动:
公共类MainActivity扩展了AppCompatActivity {
SQLiteOpenHelper dbhelper;
SQLiteDatabase db;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialisation des références
final EditText txtTo = (EditText) findViewById(R.id.TxtTO);
Button btnlogin = (Button) findViewById(R.id.btn_cnx);
TextView btnreg = (TextView) findViewById(R.id.btn_signup);
//Ouverture de la BD
dbhelper = new ModelHelper(this);
db = dbhelper.getReadableDatabase();
btnlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String TO = txtTo.getText().toString();
cursor = db.rawQuery("SELECT * FROM " + ModelHelper.TABLE_USER + " WHERE " + ModelHelper.KEY_MATRICULE + " ='?' ", new String[]{"TO"});
if (cursor != null) {
if (cursor.getCount() > 0) {
cursor.moveToFirst();
//Retrouver le TO dans la base de données USER
String nom = cursor.getString(cursor.getColumnIndex(ModelHelper.KEY_NOM));
String prenom = cursor.getString(cursor.getColumnIndex(ModelHelper.KEY_PRENOM));
//message pour affichage du msg de réussite de cnx
Toast.makeText(MainActivity.this, "Login Success", Toast.LENGTH_SHORT).show();
//Passer à la page suivante
Intent intent = new Intent(MainActivity.this, kamiActivity.class);
intent.putExtra("nom", nom);
intent.putExtra("prenom", prenom);
startActivity(intent);
//Removing MainActivity[Login Screen] from the stack for preventing back button press.
finish();
} else {
//I am showing Alert Dialog Box here for alerting user about wrong credentials
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Alert");
builder.setMessage("Votre TO n'existe pas, veuillez vous inscrire avant s'il vous plait.");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
//-------Alert Dialog Code Snippet End Here
}
}
}
});
}
public void Reg(View view) {
Intent myint = new Intent(this,signup.class);
}
}
然后是Modelhelper:
package model.sqlite.data;
import android.util.Log;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
import model.sqlite.data.user;
import model.sqlite.data.question;
import model.sqlite.data.theme;
import model.sqlite.data.answer;
public class ModelHelper extends SQLiteOpenHelper {
private static final String LOG = "ModelHelper";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "model";
// Table names
public static final String TABLE_USER = "User";
private static final String TABLE_QUESTION = "Question";
private static final String TABLE_THEME = "Theme";
private static final String TABLE_ANSWER = "Answer";
// Common column names
private static final String KEY_ID = "id";
private static final String KEY_ID_QUESTION = "ID_question";
public static final String KEY_MATRICULE = "Matricule";
private static final String KEY_ID_THEME = "ID_Theme";
// Table User columns
public static final String KEY_NOM = "Nom";
public static final String KEY_PRENOM = "Prenom";
public static final String KEY_TYPE = "Type";
// Table Question columns
private static final String KEY_QUESTION = "Question";
private static final String KEY_PROFIL_CIBLE = "Profil cible";
private static final String KEY_REGLE = "Regle";
private static final String KEY_PLANACT = "PlanAct";
// Table Theme columns
private static final String KEY_THEME = "Theme";
// Table Answer columns
private static final String KEY_DATE = "Date";
private static final String KEY_REPONSE = "Reponse";
private static final String KEY_COMMENTAIRE = "Commentaire";
// User table create statement
private static final String CREATE_TABLE_USER =
"CREATE TABLE " + TABLE_USER + "("
+ KEY_MATRICULE + "INTEGER PRIMARY KEY"
+ KEY_NOM + "TEXT"
+ KEY_PRENOM + "TEXT"
+ KEY_TYPE + "TEXT"
+ ")";
// Question table create statement
private static final String CREATE_TABLE_QUESTION =
"CREATE TABLE " + TABLE_QUESTION + "("
+ KEY_ID + " INTEGER PRIMARY KEY"
+ KEY_ID_QUESTION + "INTEGER PRIMARY KEY"
+ KEY_QUESTION + "TEXT"
+ KEY_PROFIL_CIBLE + "TEXT"
+ KEY_REGLE + "TEXT"
+ KEY_PLANACT + "TEXT"
+ KEY_ID_THEME + "INTEGER"
+ ")";
// Theme table create statement
private static final String CREATE_TABLE_THEME =
"CREATE TABLE " + TABLE_THEME + "("
+ KEY_ID + " INTEGER PRIMARY KEY"
+ KEY_ID_THEME + "INTEGER PRIMARY KEY"
+ KEY_THEME + "TEXT"
+ ")";
// Answer table create statement
private static final String CREATE_TABLE_ANSWER =
"CREATE TABLE " + TABLE_ANSWER + "("
+ KEY_ID + " INTEGER PRIMARY KEY"
+ KEY_MATRICULE + "INTEGER PRIMARY KEY"
+ KEY_ID_QUESTION + "INTEGER PRIMARY KEY"
+ KEY_DATE + "TEXT PRIMARY KEY"
+ KEY_REPONSE + "TEXT"
+ KEY_COMMENTAIRE + "TEXT"
+ ")";
public ModelHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// creating required tables
db.execSQL(CREATE_TABLE_USER);
db.execSQL(CREATE_TABLE_QUESTION);
db.execSQL(CREATE_TABLE_THEME);
db.execSQL(CREATE_TABLE_ANSWER);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUESTION);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_THEME);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ANSWER);
// create new tables
onCreate(db);
}
// closing database
public void closeDB() {
SQLiteDatabase db = this.getReadableDatabase();
if (db != null && db.isOpen())
db.close();
}
// CRUD operations for User
public long createuser(user arg) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_MATRICULE, arg.getmatricule());
values.put(KEY_NOM, arg.getnom());
values.put(KEY_PRENOM, arg.getprenom());
values.put(KEY_TYPE, arg.gettype());
return db.insert(TABLE_USER, null, values);
}
public user getuser(long id) {
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_USER + " WHERE "
+ KEY_ID + " = " + id;
Log.e(LOG, selectQuery);
Cursor c = db.rawQuery(selectQuery, null);
if (c != null) {
c.moveToFirst();
}
user entry = new user();
entry.setmatricule(c.getInt(c.getColumnIndex(KEY_MATRICULE)));
entry.setnom(c.getString(c.getColumnIndex(KEY_NOM)));
entry.setprenom(c.getString(c.getColumnIndex(KEY_PRENOM)));
entry.settype(c.getString(c.getColumnIndex(KEY_TYPE)));
return entry;
}
public List<user> getAlluser() {
List<user> list = new ArrayList<user>();
String selectQuery = "SELECT * FROM " + TABLE_USER;
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
user entry = new user();
entry.setmatricule(c.getInt(c.getColumnIndex(KEY_MATRICULE)));
entry.setnom(c.getString(c.getColumnIndex(KEY_NOM)));
entry.setprenom(c.getString(c.getColumnIndex(KEY_PRENOM)));
entry.settype(c.getString(c.getColumnIndex(KEY_TYPE)));
// adding to todo list
list.add(entry);
} while (c.moveToNext());
}
return list;
}
public long updateuser(user arg) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_MATRICULE, arg.getmatricule());
values.put(KEY_NOM, arg.getnom());
values.put(KEY_PRENOM, arg.getprenom());
values.put(KEY_TYPE, arg.gettype());
// updating row
return db.update(TABLE_USER, values, KEY_ID + " = ?",
new String[] { String.valueOf(arg.getId()) });
}
public void deleteuser(long id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_USER, KEY_ID + " = ?",
new String[] { String.valueOf(id) });
}
// CRUD operations for Question
public long createquestion(question arg) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID_QUESTION, arg.getiDQuestion());
values.put(KEY_QUESTION, arg.getquestion());
values.put(KEY_PROFIL_CIBLE, arg.getprofilCible());
values.put(KEY_REGLE, arg.getregle());
values.put(KEY_PLANACT, arg.getplanAct());
values.put(KEY_ID_THEME, arg.getiDTheme());
return db.insert(TABLE_QUESTION, null, values);
}
public question getquestion(long id) {
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_QUESTION + " WHERE "
+ KEY_ID + " = " + id;
Log.e(LOG, selectQuery);
Cursor c = db.rawQuery(selectQuery, null);
if (c != null) {
c.moveToFirst();
}
question entry = new question();
entry.setiDQuestion(c.getInt(c.getColumnIndex(KEY_ID_QUESTION)));
entry.setquestion(c.getString(c.getColumnIndex(KEY_QUESTION)));
entry.setprofilCible(c.getString(c.getColumnIndex(KEY_PROFIL_CIBLE)));
entry.setregle(c.getString(c.getColumnIndex(KEY_REGLE)));
entry.setplanAct(c.getString(c.getColumnIndex(KEY_PLANACT)));
entry.setiDTheme(c.getInt(c.getColumnIndex(KEY_ID_THEME)));
return entry;
}
public List<question> getAllquestion() {
List<question> list = new ArrayList<question>();
String selectQuery = "SELECT * FROM " + TABLE_QUESTION;
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
question entry = new question();
entry.setiDQuestion(c.getInt(c.getColumnIndex(KEY_ID_QUESTION)));
entry.setquestion(c.getString(c.getColumnIndex(KEY_QUESTION)));
entry.setprofilCible(c.getString(c.getColumnIndex(KEY_PROFIL_CIBLE)));
entry.setregle(c.getString(c.getColumnIndex(KEY_REGLE)));
entry.setplanAct(c.getString(c.getColumnIndex(KEY_PLANACT)));
entry.setiDTheme(c.getInt(c.getColumnIndex(KEY_ID_THEME)));
// adding to todo list
list.add(entry);
} while (c.moveToNext());
}
return list;
}
public long updatequestion(question arg) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID_QUESTION, arg.getiDQuestion());
values.put(KEY_QUESTION, arg.getquestion());
values.put(KEY_PROFIL_CIBLE, arg.getprofilCible());
values.put(KEY_REGLE, arg.getregle());
values.put(KEY_PLANACT, arg.getplanAct());
values.put(KEY_ID_THEME, arg.getiDTheme());
// updating row
return db.update(TABLE_QUESTION, values, KEY_ID + " = ?",
new String[] { String.valueOf(arg.getId()) });
}
public void deletequestion(long id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_QUESTION, KEY_ID + " = ?",
new String[] { String.valueOf(id) });
}
// CRUD operations for Theme
public long createtheme(theme arg) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID_THEME, arg.getiDTheme());
values.put(KEY_THEME, arg.gettheme());
return db.insert(TABLE_THEME, null, values);
}
public theme gettheme(long id) {
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_THEME + " WHERE "
+ KEY_ID + " = " + id;
Log.e(LOG, selectQuery);
Cursor c = db.rawQuery(selectQuery, null);
if (c != null) {
c.moveToFirst();
}
theme entry = new theme();
entry.setiDTheme(c.getInt(c.getColumnIndex(KEY_ID_THEME)));
entry.settheme(c.getString(c.getColumnIndex(KEY_THEME)));
return entry;
}
public List<theme> getAlltheme() {
List<theme> list = new ArrayList<theme>();
String selectQuery = "SELECT * FROM " + TABLE_THEME;
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
theme entry = new theme();
entry.setiDTheme(c.getInt(c.getColumnIndex(KEY_ID_THEME)));
entry.settheme(c.getString(c.getColumnIndex(KEY_THEME)));
// adding to todo list
list.add(entry);
} while (c.moveToNext());
}
return list;
}
public long updatetheme(theme arg) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID_THEME, arg.getiDTheme());
values.put(KEY_THEME, arg.gettheme());
// updating row
return db.update(TABLE_THEME, values, KEY_ID + " = ?",
new String[] { String.valueOf(arg.getId()) });
}
public void deletetheme(long id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_THEME, KEY_ID + " = ?",
new String[] { String.valueOf(id) });
}
// CRUD operations for Answer
public long createanswer(answer arg) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_MATRICULE, arg.getmatricule());
values.put(KEY_ID_QUESTION, arg.getiDQuestion());
values.put(KEY_DATE, arg.getdate());
values.put(KEY_REPONSE, arg.getreponse());
values.put(KEY_COMMENTAIRE, arg.getcommentaire());
return db.insert(TABLE_ANSWER, null, values);
}
public answer getanswer(long id) {
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_ANSWER + " WHERE "
+ KEY_ID + " = " + id;
Log.e(LOG, selectQuery);
Cursor c = db.rawQuery(selectQuery, null);
if (c != null) {
c.moveToFirst();
}
answer entry = new answer();
entry.setmatricule(c.getInt(c.getColumnIndex(KEY_MATRICULE)));
entry.setiDQuestion(c.getInt(c.getColumnIndex(KEY_ID_QUESTION)));
entry.setdate(c.getString(c.getColumnIndex(KEY_DATE)));
entry.setreponse(c.getString(c.getColumnIndex(KEY_REPONSE)));
entry.setcommentaire(c.getString(c.getColumnIndex(KEY_COMMENTAIRE)));
return entry;
}
public List<answer> getAllanswer() {
List<answer> list = new ArrayList<answer>();
String selectQuery = "SELECT * FROM " + TABLE_ANSWER;
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
answer entry = new answer();
entry.setmatricule(c.getInt(c.getColumnIndex(KEY_MATRICULE)));
entry.setiDQuestion(c.getInt(c.getColumnIndex(KEY_ID_QUESTION)));
entry.setdate(c.getString(c.getColumnIndex(KEY_DATE)));
entry.setreponse(c.getString(c.getColumnIndex(KEY_REPONSE)));
entry.setcommentaire(c.getString(c.getColumnIndex(KEY_COMMENTAIRE)));
// adding to todo list
list.add(entry);
} while (c.moveToNext());
}
return list;
}
public long updateanswer(answer arg) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_MATRICULE, arg.getmatricule());
values.put(KEY_ID_QUESTION, arg.getiDQuestion());
values.put(KEY_DATE, arg.getdate().toString());
values.put(KEY_REPONSE, arg.getreponse());
values.put(KEY_COMMENTAIRE, arg.getcommentaire());
// updating row
return db.update(TABLE_ANSWER, values, KEY_ID + " = ?",
new String[] { String.valueOf(arg.getId()) });
}
public void deleteanswer(long id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_ANSWER, KEY_ID + " = ?",
new String[] { String.valueOf(id) });
}
}
我看到问题是使用int:matricule,但我检查了我在代码中没有发现错误或问题的地方。 感谢
答案 0 :(得分:1)
这是实际错误:
java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.root.myapplication / com.example.root.myapplication.MainActivity}: android.database.sqlite.SQLiteException:靠近“KEYMatriculeINTEGER”: 语法错误(代码1):,编译时:CREATE TABLE User(id INTEGER PRIMARY KEYMatriculeINTEGER PRIMARY KEYNOMTEXTPrenomTEXTTypeTEXT)
你遗漏了一些逗号:
private static final String CREATE_TABLE_USER =
"CREATE TABLE " + TABLE_USER + "("
+ KEY_MATRICULE + " INTEGER PRIMARY KEY, "
+ KEY_NOM + " TEXT, "
+ KEY_PRENOM + " TEXT, "
+ KEY_TYPE + " TEXT"
+ ")";