本帖最后由 napo815 于 2012-08-09 18:03:13 编辑

解决方案 »

  1.   

    你在表里面定义id是自动生成的,那么ContentValues cv = new ContentValues();
            cv.put(Song_id, _id);
            cv.put(Song_Name, SongName);
            cv.put(Song_Url, SongUrl);
            cv.put(Song_Type, SongType);
            System.out.println("cv------>" + cv);
            return db.insert(TB_SONG, null, cv); 这个地方要改成:ContentValues cv = new ContentValues();
            cv.put(Song_Name, SongName);
            cv.put(Song_Url, SongUrl);
            cv.put(Song_Type, SongType);
            System.out.println("cv------>" + cv);
            return db.insert(TB_SONG, "SongName, SongUrl, SongType", cv); 
      

  2.   

    不行啊, 照你的改了之后,还是和之前一样,报错和log 都一模一样
      

  3.   

    那说明db对象未初始化,MainActivity.java文件发上来看看你是如何调用的
      

  4.   


    package com.xiuman.XMPlayer_DB;import android.os.Bundle;
    import android.app.Activity;import android.database.sqlite.SQLiteDatabase;
    import com.xiuman.XMPlayer_DB.DatabaseHelper;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;public class MainActivity extends Activity {

    private Button createButton;
    private Button insertButton;
    private Button updateButton;
    private Button updateRecordButton;
    private Button queryButton;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            createButton = (Button)findViewById(R.id.createDatabase);
            updateButton = (Button)findViewById(R.id.updateDatabase);
            insertButton = (Button)findViewById(R.id.insert);
            updateRecordButton = (Button)findViewById(R.id.update);
            queryButton = (Button)findViewById(R.id.query);
            createButton.setOnClickListener(new CreateListener());
            updateButton.setOnClickListener(new UpdateListener());
            insertButton.setOnClickListener(new InsertListener());
            updateRecordButton.setOnClickListener(new UpdateRecordListener());
            queryButton.setOnClickListener(new QueryListener());
            DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this, "XWPlayer_DB");
        }
        class CreateListener implements OnClickListener{
    public void onClick(View v) {
    DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this,"XWPlayer_DB");
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    }
        }
        class UpdateListener implements OnClickListener{ public void onClick(View v) {
    DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this,"XWPlayer_DB",2);
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    }
        
        }
        class InsertListener implements OnClickListener{ public void onClick(View v) {
    DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this,"XWPlayer_DB", 2);
    dbHelper.insertSong(1, "zhangsan", "asd", "guonei");
    }
        }
      
        class UpdateRecordListener implements OnClickListener{ public void onClick(View arg0) {
    // TODO Auto-generated method stub
    DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this,"XWPlayer_DB");
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    dbHelper.updateSong(12, "zhangsanfeng", "asd", "国内");
    }
        }
        class QueryListener implements OnClickListener{ public void onClick(View v) {
    System.out.println("aaa------------------");
    Log.d("myDebug", "myFirstDebugMsg");

    DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this,"XWPlayer_DB");
    dbHelper.selectSong();
    }
        }
        
    }
      

  5.   


    ContentValues cv = new ContentValues();
       cv.put(Song_id, _id);
       cv.put(Song_Name, SongName);
       cv.put(Song_Url, SongUrl);
       cv.put(Song_Type, SongType);
       System.out.println("cv------>" + cv);
       return db.insert(TB_SONG, null, cv);  这个地方要改成:ContentValues cv = new ContentValues();
         cv.put(Song_Name, SongName);
         cv.put(Song_Url, SongUrl);
         cv.put(Song_Type, SongType);
         System.out.println("cv------>" + cv);
         open();
         return db.insert(TB_SONG, "SongName, SongUrl, SongType", cv); 
      

  6.   

    改成你这样之后,又报错了,这次是open();方法里面的。
    59行:db = DBHelper.getWritableDatabase(); 出错!
    log:08-09 15:35:51.208: E/AndroidRuntime(525): Uncaught handler: thread main exiting due to uncaught exception
    08-09 15:35:51.248: E/AndroidRuntime(525): java.lang.NullPointerException
    08-09 15:35:51.248: E/AndroidRuntime(525):  at com.xiuman.XMPlayer_DB.DatabaseHelper.open(DatabaseHelper.java:59)
    08-09 15:35:51.248: E/AndroidRuntime(525):  at com.xiuman.XMPlayer_DB.DatabaseHelper.insertSong(DatabaseHelper.java:79)
    08-09 15:35:51.248: E/AndroidRuntime(525):  at com.xiuman.XMPlayer_DB.MainActivity$InsertListener.onClick(MainActivity.java:54)
      

  7.   

        public DatabaseHelper(Context context, String name) {
            this(context, DB_NAME, VERSION);
        }
        
        public DatabaseHelper(Context context, String name, int version)
        {
            this(context, DB_NAME, null, VERSION);
        }
    改为:    public DatabaseHelper(Context context, String name) {
            this(context, DB_NAME, VERSION);
            DBHelper = new DatabaseHelper(context, DB_NAME, null, VERSION);
        }
        
        public DatabaseHelper(Context context, String name, int version)
        {
            this(context, DB_NAME, null, VERSION);
            DBHelper = new DatabaseHelper(context, DB_NAME, null, VERSION);
        }看看
      

  8.   

    OK啦,谢啦 
    插入和更新都可以;饿  现在就查询还是报错
    134行:        return db.query(TB_SONG, new String[]{"_id","SongName","SongUrl", "SongType"}, "id=?", new String[]{"1"}, null, null, " _id desc");
    log:08-09 16:45:29.185: E/AndroidRuntime(647): Uncaught handler: thread main exiting due to uncaught exception
    08-09 16:45:29.225: E/AndroidRuntime(647): android.database.sqlite.SQLiteException: no such column: id: , while compiling: SELECT _id, SongName, SongUrl, SongType FROM Song_TB WHERE id=? ORDER BY  _id desc
    08-09 16:45:29.225: E/AndroidRuntime(647):  at android.database.sqlite.SQLiteProgram.native_compile(Native Method)
    08-09 16:45:29.225: E/AndroidRuntime(647):  at android.database.sqlite.SQLiteProgram.compile(SQLiteProgram.java:110)
    08-09 16:45:29.225: E/AndroidRuntime(647):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
    08-09 16:45:29.225: E/AndroidRuntime(647):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
    08-09 16:45:29.225: E/AndroidRuntime(647):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:49)
    08-09 16:45:29.225: E/AndroidRuntime(647):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1221)
    08-09 16:45:29.225: E/AndroidRuntime(647):  at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1108)
    08-09 16:45:29.225: E/AndroidRuntime(647):  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1066)
    08-09 16:45:29.225: E/AndroidRuntime(647):  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1143)
    08-09 16:45:29.225: E/AndroidRuntime(647):  at com.xiuman.XMPlayer_DB.DatabaseHelper.selectSong(DatabaseHelper.java:134)
    08-09 16:45:29.225: E/AndroidRuntime(647):  at com.xiuman.XMPlayer_DB.MainActivity$QueryListener.onClick(MainActivity.java:74)