找不到符号方法execSQL(String)

时间:2014-11-21 10:34:46

标签: java android sql methods exec

我正在为我的应用程序做SQLite数据库部分,我需要帮助。我想清除数据库中的所有数据,但是当我实现这个方法时:

public void cleardb() {
        db.execSQL("delete from" + DATABASE_TABLE);
    }

它给我一个这样的错误,

Error:(25, 11) error: cannot find symbol method execSQL(String)

请详细指导我。

这是我的数据库完整代码

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

public class WordDatabase {

    public static final String WORD_ID = "_id";
    public static final String WORD_TOTRANSLATE = "to_translate";
    public static final String WORD_TRANSLATED = "translated";

    private static final String DATABASE_NAME = "WordHistory";
    private static final String DATABASE_TABLE = "WordTable";
    private static final int DATABASE_VERSION = 1;

    private Database db;
    private final Context cont;
    private SQLiteDatabase HistoryDatabase;

    public void cleardb() {
        db.execSQL("delete from" + DATABASE_TABLE);
    }


    public static class Database extends SQLiteOpenHelper{

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

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                            WORD_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                            WORD_TOTRANSLATE + " TEXT NOT NULL, " +
                            WORD_TRANSLATED + " 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 WordDatabase(Context con){
        cont = con;
    }

    public WordDatabase open() throws Exception{
        db = new Database(cont);
        HistoryDatabase = db.getWritableDatabase();
        return this;
    }

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

    public void createEntry(String beforeTranslate, String afterTranslate) {
        // TODO Auto-generated method stub
        ContentValues content = new ContentValues();
        content.put(WORD_TOTRANSLATE, beforeTranslate);
        content.put(WORD_TRANSLATED, afterTranslate);
        HistoryDatabase.insert(DATABASE_TABLE, null, content);
    }

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


        String[] col = new String[]{WORD_ID, WORD_TOTRANSLATE, WORD_TRANSLATED};
        Cursor curs = HistoryDatabase.query(DATABASE_TABLE, col, null, null, null, null, null);
        String res = "";
        int rowNum = curs.getColumnIndex(WORD_ID);
        int toTranslateNum = curs.getColumnIndex(WORD_TOTRANSLATE);
        int TranslatedNum = curs.getColumnIndex(WORD_TRANSLATED);

        for(curs.moveToFirst(); !curs.isAfterLast(); curs.moveToNext()){
            res = res + curs.getString(rowNum) + " " + curs.getString(toTranslateNum) + "          " + curs.getString(TranslatedNum) + "\n";
        }

        return res;
    }


}

将数据存储到数据库中的侦听器:

translate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                class runTranslation extends AsyncTask<Void,Void,Void>{

                    @Override
                    protected void onPreExecute() {
                        // TODO Auto-generated method stub
                        pbTranslate.setVisibility(ProgressBar.VISIBLE);
                        super.onPreExecute();
                    }

                    @Override
                    protected Void doInBackground(Void... params) {
                        // TODO Auto-generated method stub
                        try {
                            output = translate();

                        } catch (Exception e) {
                            e.printStackTrace();

                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result) {
                        // TODO Auto-generated method stub
                        pbTranslate.setVisibility(ProgressBar.INVISIBLE);
                        tled.setText(output);

                        super.onPostExecute(result);

                        try{
                            String beforeTranslate = toTl.getText().toString();
                            String afterTranslate = output;

                            WordDatabase newEntry = new WordDatabase(MainActivity.this);
                            newEntry.open();
                            newEntry.createEntry(beforeTranslate, afterTranslate);
                            newEntry.close();
                        }catch(Exception e){
                            e.printStackTrace();
                        }

我在尝试了@johny kumar给出的代码后得到了这个错误:

11-21 20:41:38.584  27938-27938/fyp.atms.juicy.utranslator W/System.err﹕ java.lang.NullPointerException
11-21 20:41:38.584  27938-27938/fyp.atms.juicy.utranslator W/System.err﹕ at fyp.atms.juicy.utranslator.WordDatabase.close(WordDatabase.java:68)
11-21 20:41:38.584  27938-27938/fyp.atms.juicy.utranslator W/System.err﹕ at fyp.atms.juicy.utranslator.MainActivity$1$1runTranslation.onPostExecute(MainActivity.java:129)
11-21 20:41:38.584  27938-27938/fyp.atms.juicy.utranslator W/System.err﹕ at fyp.atms.juicy.utranslator.MainActivity$1$1runTranslation.onPostExecute(MainActivity.java:92)
11-21 20:41:38.584  27938-27938/fyp.atms.juicy.utranslator W/System.err﹕ at android.os.AsyncTask.finish(AsyncTask.java:632)
11-21 20:41:38.584  27938-27938/fyp.atms.juicy.utranslator W/System.err﹕ at android.os.AsyncTask.access$600(AsyncTask.java:177)

第129行:

                    newEntry.close();

第92行:

        class runTranslation extends AsyncTask<Void,Void,Void>{

WordDatabase第68行:(来自第一个代码块)

database.close();

4 个答案:

答案 0 :(得分:0)

我怀疑缺少“空间”:

db.execSQL("delete from" + DATABASE_TABLE);

应该是:

db.execSQL("delete from " + DATABASE_TABLE);

答案 1 :(得分:0)

I hope this should work.

package com.example.sample;

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

公共类WordDatabase {

public static final String WORD_ID = "_id";
public static final String WORD_TOTRANSLATE = "to_translate";
public static final String WORD_TRANSLATED = "translated";

private static final String DATABASE_NAME = "WordHistory";
private static final String DATABASE_TABLE = "WordTable";
private static final int DATABASE_VERSION = 1;

private SQLiteDatabase database;
private final Context cont;
private Database dbHelper = null;
private SQLiteDatabase HistoryDatabase;

public void cleardb() {
    database.execSQL("delete from" + DATABASE_TABLE);
}


public static class Database extends SQLiteOpenHelper{

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                        WORD_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                        WORD_TOTRANSLATE + " TEXT NOT NULL, " +
                        WORD_TRANSLATED + " 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 WordDatabase(Context con){
    cont = con;


}

public WordDatabase open() throws Exception{
    dbHelper = new Database(cont);
    database = dbHelper.getWritableDatabase();
    return this;
}

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

public void createEntry(String beforeTranslate, String afterTranslate) {
    // TODO Auto-generated method stub
    ContentValues content = new ContentValues();
    content.put(WORD_TOTRANSLATE, beforeTranslate);
    content.put(WORD_TRANSLATED, afterTranslate);
    HistoryDatabase.insert(DATABASE_TABLE, null, content);
}

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


    String[] col = new String[]{WORD_ID, WORD_TOTRANSLATE, WORD_TRANSLATED};
    Cursor curs = HistoryDatabase.query(DATABASE_TABLE, col, null, null, null, null, null);
    String res = "";
    int rowNum = curs.getColumnIndex(WORD_ID);
    int toTranslateNum = curs.getColumnIndex(WORD_TOTRANSLATE);
    int TranslatedNum = curs.getColumnIndex(WORD_TRANSLATED);

    for(curs.moveToFirst(); !curs.isAfterLast(); curs.moveToNext()){
        res = res + curs.getString(rowNum) + " " + curs.getString(toTranslateNum) + "          " + curs.getString(TranslatedNum) + "\n";
    }

    return res;
}

}

答案 2 :(得分:0)

这应该可以解决问题:

 db.delete(TABLE_NAME,null, null);

答案 3 :(得分:0)

试试这个 db.execSQL(&#34;删除&#34; + DATABASE_TABLE +&#34 ;;&#34;);

在表名后添加半号