简体   繁体   中英

SQLite Android error code1 when CREATING TABLE

I've been going through the buckys tutorials several times but can't still find the problem. I took a screenshot from my error message but could not post it here. Could someone please help me because I been stuck with this problem for several days now.

The error message is:

android.database.sqlite.SQLiteException near "CREATE TABLE peopleTable ":syntax error (code1):, while compiling: CREATE TABLE peopleTable (_id INTEGER PRIMARY KEY AUTOINCREMENT, persons_name TEXT NOT NULL persons_hotness TEXT NOT NULL);

Here is the code for HotOrNot class:

    package com.peltho.momskoll;

    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteDatabase.CursorFactory;
    import android.database.sqlite.SQLiteOpenHelper;

    public class HotOrNot {

    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "persons_name";
    public static final String KEY_HOTNESS = "persons_hotness";

    private static final String DATABASE_NAME = "HotOrNotdb";
    private static final String DATABASE_TABLE = "peopleTable";
    private static final int DATABASE_VERSION = 1;

    private DbHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;

    private static class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    // denna klasss anv nds endast f rsta g ngen databasen tas i
    // anv ndning
    db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
    + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME
    + " TEXT NOT NULL, " + KEY_HOTNESS + " TEXT NOT NULL);");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
    onCreate(db);
    }

    }

    public HotOrNot(Context c) {
    ourContext = c;

    }

    public HotOrNot open() throws SQLException {
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
    }

    public void close() {
    ourHelper.close();
    }

    public long createEntry(String name, String hotness) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_NAME, name);
    cv.put(KEY_HOTNESS, hotness);
    // cv.put(KEY_VAT, vat);
    // cv.put(KEY_DATE, date);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);

    }

    public String getData() {
    // TODO Auto-generated method stub

    String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_HOTNESS };
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
    null, null);

    String result = "";
    int iRow = c.getColumnIndex(KEY_ROWID);
    int iName = c.getColumnIndex(KEY_NAME);
    int iHotness = c.getColumnIndex(KEY_HOTNESS);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
    result = result + c.getString(iRow) + " " + c.getString(iName)
    + " " + c.getString(iHotness) + "\n";
    }

    return result;
    }
   }

My code in SQLiteExempel

package com.peltho.momskoll;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SQLiteExample extends Activity implements OnClickListener {

Button sqlUpdate, sqlView;
EditText sqlName, sqlHotness;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sqliteexample);
sqlUpdate = (Button) findViewById(R.id.bSQLUpdate);
sqlView = (Button) findViewById(R.id.bSQLOpenView);
sqlName = (EditText) findViewById(R.id.etSQLName);
sqlHotness = (EditText) findViewById(R.id.etSQLHotness);
// sqlVat = (EditText) findViewById(R.id.etSQLVAT);
// sqlDate = (EditText) findViewById(R.id.etSQLDate);
sqlView.setOnClickListener(this);
sqlUpdate.setOnClickListener(this);

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bSQLUpdate:

boolean didItWork = true;
try {
String name = sqlName.getText().toString();
String hotness = sqlHotness.getText().toString();
// String va = sqlVat.getText().toString();
// String date = sqlDate.getText().toString();
// sum = Double.parseDouble(su);
// vat = Double.parseDouble(va);

HotOrNot entry = new HotOrNot(SQLiteExample.this);
entry.open();
entry.createEntry(name, hotness);
entry.close();
} catch (Exception e) {

//It gives me this error message
didItWork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("NOO");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();

} finally {

//It is supposed to do thisif it works
if (didItWork) {
Dialog d = new Dialog(this);
d.setTitle("Jeeep");
TextView tv = new TextView(this);
tv.setText("Lyckades");
d.setContentView(tv);
d.show();
}
}

break;
case R.id.bSQLOpenView:
Intent i = new Intent("com.peltho.momskoll.SQLVIEW");
startActivity(i);
break;
}
}

}

android.database.sqlite.SQLiteException near "CREATE TABLE peopleTable ":syntax error (code1):, while compiling: CREATE TABLE peopleTable (_id INTEGER PRIMARY KEY AUTOINCREMENT, persons_name TEXT NOT NULL persons_hotness TEXT NOT NULL);

near "CREATE TABLE peopleTable " indicates it is read as one token so the spaces there are not regular space characters but eg non-breaking space characters that are not token separators. Replace them with regular space characters.

There's another syntax error: missing , before persons_hotness but you'll get the error about it only later.

I forgot to type the comma when i wrote the error message, it was not in the code.

The problem was in the spaces. I thought the ctrl F had fixed that but I had to check all the spaces manually and then it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM