mardi 8 septembre 2015

E/SQLiteLog﹕ (1) near "Table": syntax error

I'm trying to start with SQLite in android but i have some problems.. I took the code from a tutorial that has written in 2012 but it's not working for me now and show me this error:

E/SQLiteLog﹕ (1) near "Table": syntax error

the problem is with - "Create/Open Database Problem."

package db.com.example.kids1.databasetest;

import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.io.IOException;
import java.util.ArrayList;

public class MainActivity extends ListActivity{

    private final String DB_NAME = "Database";
    private final String TABLE_NAME = "Table";
    SQLiteDatabase DB = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayList<String> results = new ArrayList<>();

        String[] res = {"Red", "Green", "Text"};



        try {
            DB = this.openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);
            DB.execSQL("CREATE TABLE IF NOT EXISTS " +
                TABLE_NAME +
                "(Name VARCHAR, Street VARCHAR, Block INT, City VARCHAR, Tel VARCHAR);");
            mFillDbsTable();

            Cursor c = DB.rawQuery("SELECT Name, Street, Block, City, Tel FROM " +
                    TABLE_NAME +
                    " where Blcok == 9 LIMIT 5", null);
            if (c!=null){

                if (c.moveToFirst()) {
                    do {

                        String name = c.getString(c.getColumnIndex("Name"));
                        String street = c.getString(c.getColumnIndex("Street"));
                        int block = c.getInt(c.getColumnIndex("Block"));
                        String city = c.getString(c.getColumnIndex("City"));
                        String tel = c.getString(c.getColumnIndex("Tel"));

                        results.add(name + "," + street + "," + block + "," + city + "," + tel);
                    } while (c.moveToNext());
                }
            }

            ListView list = (ListView)findViewById(android.R.id.list);


            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, res);

            list.setAdapter(adapter);

        } catch (SQLiteException se){
            Log.e(getClass().getSimpleName(), "Create/Open Database Problem.");
        }

    }

    private void mFillDbsTable(){

        try {
            DB.execSQL("INSERT INTO " +
                    TABLE_NAME +
                    " Values('Noam', 'Shkolnik', 9, 'Rehovot', '054-4900807');");

            DB.execSQL("INSERT INTO " +
                    TABLE_NAME +
                    " Values('Eyal', 'Shkolnik', 9, 'Rehovot', '055-4488779');");

            DB.execSQL("INSERT INTO " +
                    TABLE_NAME +
                    " Values('Yehontan', 'Shkolnik', 9, 'Rehovot', '058-7789547');");
        } catch (SQLiteException se) {
            Log.e(getClass().getSimpleName(), "Could not create records.");
        }

    }



}

Aucun commentaire:

Enregistrer un commentaire