mardi 1 mars 2016

syntax error (code 1): , while compiling:

I get the following error

android.database.sqlite.SQLiteException: near "TEXT": syntax error (code 1): , while compiling: CREATE TABLE contacts (name TEXT, phone INTEGER, email TEXT

The specific line of code generating the error is

sqLiteDatabase.execSQL("CREATE TABLE contacts (name TEXT, phone INTEGER, email TEXT");

Here is the full code:

package ca.truewebdev.sqlitetest1;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SQLiteDatabase sqLiteDatabase = getBaseContext().openOrCreateDatabase("sqlite-test-1.db", MODE_PRIVATE, null);
    sqLiteDatabase.execSQL("CREATE TABLE contacts (name TEXT, phone INTEGER,email TEXT");
    sqLiteDatabase.execSQL("INSERT INTO contacts VALUES('tim',6456789,'tim@email/com'");
    sqLiteDatabase.execSQL("INSERT INTO contacts VALUES('chris',324243,'chris@email/com'");

    //set up to be able to access data
    Cursor query = sqLiteDatabase.rawQuery("SELECT * FROM contacts", null);

    //check to see if any data, moves to first record or returns false if none
    if (query.moveToFirst()) {
        // cycle through all records
        String name = query.getString(0);
        int phone = query.getInt(1);
        String email = query.getString(2);
        Toast.makeText(getBaseContext(), "Name = " + name + " phone = " + phone +
                " email = " + email, Toast.LENGTH_LONG).show();

    }else{
        Toast.makeText(getBaseContext(), "Error retrieving data", Toast.LENGTH_LONG).show();
    }

}
}

Why am I getting this error? I am following along with a tutorial. I was pretty sure that I typed everything in correctly.

Aucun commentaire:

Enregistrer un commentaire