public long insertData(String name, String number, String email, String jobtype, String uri, String msg)
{
SQLiteDatabase db = database.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(database.NAME, name);
contentValues.put(database.NUMBER, number);
contentValues.put(database.EMAIL, email);
contentValues.put(database.JOBTYPE, jobtype);
contentValues.put(database.URI, uri);
contentValues.put(database.MSG, msg);
long id = db.insert(database.TABLE_NAME, null, contentValues);
return id;
}
static class Database extends SQLiteOpenHelper{
private static final String DATABASE_NAME="cloudicadatabase";
private static final String TABLE_NAME="CLTABLE";
private static final int DATABASE_VERSION= 1;
private static final String COLUMN_UID = "_id";
private static final String NAME="Name";
private static final String NUMBER="Number";
private static final String EMAIL="Email";
private static final String JOBTYPE="Jobtype";
private static final String URI="Uri";
private static final String MSG="Msg";
private static final String CREATE_TABLE = "CREATE TABLE" + TABLE_NAME + "(" +
COLUMN_UID + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
NAME + "VARCHAR(32) NOT NULL, " +
NUMBER + "INTEGER, " +
EMAIL + "VARCHAR(25) NOT NULL, " +
JOBTYPE + "VARCHAR(20) NOT NULL, " +
URI + "VARCHAR(50) NOT NULL, " +
MSG + "VARCHAR(500) NOT NULL);";
private static final String DROP_TABLE = "DROP TABLE "+TABLE_NAME+" IF EXISTS";
private Context context;
public Database(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE);
} catch (SQLException e) {
Message.message(context, ""+e);
}
}
main.java
public void submit(View view)
{
String nam = name.getText().toString();
String numb = num.getText().toString();
String mail = email.getText().toString();
String jtype = job.getText().toString();
String ur = uri.getText().toString();
String msag = msg.getText().toString();
long id = database.insertData(nam, numb, mail, jtype, ur, msag);
if (id < 0)
{
Message.message(this, "Unsucessful");
}
else {
Message.message(this, "Submited");
}
logcat
no such table: CLTABLE
12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase: Error inserting Jobtype=dfg Msg=gfdgfd Number=543 Uri=gfd Email=fdgf Name=sau
12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase: android.database.sqlite.SQLiteException: no such table: CLTABLE (code 1): , while compiling: INSERT INTO CLTABLE(Jobtype,Msg,Number,Uri,Email,Name) VALUES (?,?,?,?,?,?)
12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase: at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase: at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase: at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase: at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase: at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase: at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase: at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase: at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase: at com.sun.suni.design.DataAdapter.insertData(DataAdapter.java:30)
12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase: at com.sun.suni.design.Details.submit(Details.java:36)
12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase: at java.lang.reflect.Method.invoke(Native Method)
12-24 12:32:17.794 13270-13270/com.sun.suni.design E/SQLiteDatabase: at java.lang.reflect.Method.invoke(Method.java:372)
答案 0 :(得分:7)
您的表格未创建,因此您获得了NPE
。
您应该在Column Name
和Column Type
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" +
COLUMN_UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
NAME + " VARCHAR(32) NOT NULL, " +
NUMBER + " INTEGER, " +
EMAIL + " VARCHAR(25) NOT NULL, " +
JOBTYPE + " VARCHAR(20) NOT NULL, " +
URI + " VARCHAR(50) NOT NULL, " +
MSG + " VARCHAR(500) NOT NULL);";
答案 1 :(得分:0)
在&#34; CREATE TABLE&#34;之后,你错过了一个空格。 :
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" +
COLUMN_UID + "INTEGER PRIMARY KEY AUTOINCREMENT, " +
NAME + "VARCHAR(32) NOT NULL, " +
NUMBER + "INTEGER, " +
EMAIL + "VARCHAR(25) NOT NULL, " +
JOBTYPE + "VARCHAR(20) NOT NULL, " +
URI + "VARCHAR(50) NOT NULL, " +
MSG + "VARCHAR(500) NOT NULL);";